質問

My HTML looks like this, and I'm working with the YUI library:

<div>
    <img class="smallimage" src="host/smallimage.jpg">
</div>
<div>
    <img src="host/bigimage.jpg">
</div>

I would like to know how to
(1) add a class to the container off all images with the class of .smallimage as well as
(2) add a class to the container of all images with the string 'big' in the source tag. :)

So that the output is like this:

<div class = "small">
    <img class="smallimage" src="host/smallimage.jpg">
</div>
<div class = "big">
    <img src="host/bigimage.jpg">
</div>

Thanks very much fellas!

UPDATE: Fellas I think I have figured this out now, but would still apprecate if someone could maybe just look if its solid:

1)

YUI().use('node', function(Y)    
 var node = Y.one(".smallimage")
 Y.one(".smallimage").get('parentNode').addClass("small");
});

2)

YUI().use('node', function(Y)    
 var node = Y.one("img[src*='big']")
 Y.one("img[src*='big']").get('parentNode').addClass("big");
});

Thanks!

役に立ちましたか?

解決

Your solution is correct, but as the function says using Y.one will affect only one element. If you have multiple images you should use Y.all.

Also, keep in mind that you can do everything inside one YUI().use() call.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top