如何添加checked=“checked” .html() 方法的输出的属性
无论是在 jQuery 级别还是在 DOM 级别,复选框的 selected 属性一定有一些特殊之处。使用这个 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:\n\n"
+ $("body").html());
});
我没有得到我所期望的;如果我使用“checked”之外的属性名称,它会按预期工作。
我试图让它工作的原因是我需要获取浏览器 DOM 中存在的 HTML 并将其转换为 PDF,以便用户可以获得页面的 PDF 版本,就像他们正在查看的那样;它在客户端被大量操作,因此直接从服务器获取正确的 HTML 并不是一个简单的选择。
这是上述代码的工作版本: http://jsfiddle.net/pettys/qTfDP/2/< /a>
There 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/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更新至 jQuery 1.6。
attr
发生了一些重大变化1.6/1.6.1。Update to jQuery 1.6. There have been some significant changes to
attr
in 1.6/1.6.1.