English 中文(简体)
如何将checked=“checked”属性添加到.html()方法的输出中
原标题:How to add the checked="checked" attribute to the output of .html() method

在jQuery级别或DOM级别,复选框的checked属性一定有一些特殊之处。使用此HTML:

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

这个JavaScript:

$(function() {

    $("#cb").attr("checked","checked");
    alert("Expecting this HTML fragment to have a  checked  attribute:

"
          + $("body").html());

});

我没有得到我所期望的;如果我使用除“checked”之外的属性名称,它将按预期工作。

我试图让它发挥作用的原因是,我需要获得浏览器DOM中存在的HTML,并将其转换为PDF,这样用户就可以在查看页面时获得PDF版本;它在客户端被大量操作,因此直接从服务器获得正确的HTML不是一个容易的选择。

这是上面代码的工作版本:http://jsfiddle.net/pettys/qTfDP/2/

最佳回答

更新到jQuery 1.6。出现了一些在1.6/1.6.1中对attr进行了重大更改。

问题回答

暂无回答




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签