当前位置: 动力学知识库 > 问答 > 编程问答 >

html - jQuery img with class .css not working

问题描述:

I've been looking for hours now why the following code won't work:

naam = "div#" + $(naam).next('div').attr('id');

current = "img." + $(naam).attr('id');

$(current).css("border", "solid 4px #9966FF");

The div has an id with the same name as the class from the img:

<div id="michiel">

This is the image I want to use the code on:

<img src="Images/michielsmall.jpg" class="michiel, link" alt="Michiel">

As you can see I'm trying to change the border of the img with class "michiel".

When I console.log(current);, Output is: img.michiel, which is correct.

I really don't know why this won't work, please help.

网友答案:

That is because of the comma in the class name

class="michiel, link"
              ^^------- Remove this

Supposed to be

class="michiel link"

OR .. If you want the comma to be present , then change your selector to

var current = "img." + $(naam).attr('id') + "\\,";

But this is not recommended

Check Fiddle

网友答案:

naam is not defined. I made a jsfiddle here and looked in the chrome console.

Update

I changed your javascript to $('.michiel').css('border', 'solid 4px #9966FF'); If you need it to be more specific, I'm sure you can take it from here.

http://jsfiddle.net/jmutv/2/

分享给朋友:
您可能感兴趣的文章:
随机阅读: