admin管理员组

文章数量:1278822

I'm trying to change HTML attributes using jQuery, but no matter what I do, I can't get anything to work with jQuery's .attr().

For testing, I've written

alert($("#logo").attr("title"));

and even though I have an img with id="logo" and a title, the alert remains blank.

My full method:

function switchVideo(videoID) {
    var videoPlayer = document.getElementById("example_video_1");
    videoPlayer.src = "video/Occam.webm";
    videoPlayer.load();
    $("#example_video_1").attr("poster", "video/ss.png");

    //Doesn't work:
    alert($("#logo").attr("title"));
    $("#logo").fadeOut();

    videoPlayer.play();
}

My fade out works, so I know I imported jQuery correctly.

I've gotten it working in another document, so there must be something else in the document messing it up. Does anyone know why this simple method won't work?

You can see the source page at .html

I'm trying to change HTML attributes using jQuery, but no matter what I do, I can't get anything to work with jQuery's .attr().

For testing, I've written

alert($("#logo").attr("title"));

and even though I have an img with id="logo" and a title, the alert remains blank.

My full method:

function switchVideo(videoID) {
    var videoPlayer = document.getElementById("example_video_1");
    videoPlayer.src = "video/Occam.webm";
    videoPlayer.load();
    $("#example_video_1").attr("poster", "video/ss.png");

    //Doesn't work:
    alert($("#logo").attr("title"));
    $("#logo").fadeOut();

    videoPlayer.play();
}

My fade out works, so I know I imported jQuery correctly.

I've gotten it working in another document, so there must be something else in the document messing it up. Does anyone know why this simple method won't work?

You can see the source page at http://jrstrauss/temp/create.html

Share Improve this question edited Jan 4, 2012 at 14:13 Rory McCrossan 338k41 gold badges320 silver badges351 bronze badges asked Jan 4, 2012 at 14:08 Russell StraussRussell Strauss 1,2003 gold badges14 silver badges32 bronze badges 1
  • when is this function called? – Daniel A. White Commented Jan 4, 2012 at 14:10
Add a ment  | 

5 Answers 5

Reset to default 8

Your div has the id logo, not the img. Try:

$("#logo img").attr("title")

You should be using $.fn.prop for that now: http://api.jquery./prop/

You should use prop if you are using a recent version of jQuery.

You can also try this according to your HTML:

alert( $("#logo a img").attr("title") );

Demo

You have the following markup with id logo

<div id="logo">
......
</div>

Now, you are trying to run jQuery's .attr method by following code.

$("#logo").attr("title");

as you may know .attr method retrieves attribute value of the given element but that <div> doesn't have a attribute named title. so to confirm this, try retrieving attribute that does exist, for example id. so try this

alert($("#logo").attr("id"));

This will return the value of attribute. the important thing to note is jQuery looks for attributes of given element only, It doesn't scan child elements.

So, to make it work in your case. You need to do the following

alert($("#logo img").attr("title"));

Hope it helps

本文标签: javascriptJQuery attr won39t retrieve or change valuesStack Overflow