开发者

How to add the checked="checked" attribute to the output of .html() method

There开发者_如何学C must be something special about the checked attribute of a checkbox either at the jQuery level or the DOM level. With this HTML:

<html>
    <body>
        <input id="cb" type="checkbox" />
    </body>
</html>

And this JavaScript:

$(function() {

    $("#cb").attr("checked","checked");
    alert("Expecting this HTML fragment to have a 'checked' attribute:\n\n"
          + $("body").html());

});

I'm not getting what I expect; if I use an attribute name besides "checked" it works as expected.

The reason I'm trying to get this to work is I need to get the HTML as it exists in a browser DOM and convert it to PDF, so users can get PDF versions of the page as it is as they're looking at it; it's manipulated heavily on the client side, so getting the correct HTML directly from the server is not an easy option.

Here's a working version of the above code: http://jsfiddle.net/pettys/qTfDP/2/


Update to jQuery 1.6. There have been some significant changes to attr in 1.6/1.6.1.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜