문제

I have the following HTML:

<div>
    <img src="source.com/image.jpg" title="My Image">
</div>

I would like to get the title of all images ("My image") in this case, and add this title on to the parent , so that the above example would look like this:

<div title="My Image">
    <img src="source.com/image.jpg" title="My Image">
</div>

Would appreciate it very much if someone could give me a push in the right direction!

Thanks!

도움이 되었습니까?

해결책

This is pretty much the same question as the previous one you posted. In this case you do need to save the image node to a variable so that you can read its title and set it to the parent.

YUI().use('node', function (Y) {
  var image = Y.one('img');
  image.get('parentNode').set('title', image.get('title'));
});

If you're having trouble making changes to DOM elements I'd suggest you take a little time to read a couple of articles/books that introduce you to the subject. Here are some good options:

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top