如何添加checked=“checked” .html() 方法的输出的属性

发布于 2024-11-10 11:26:15 字数 731 浏览 7 评论 0原文

无论是在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

饮湿 2024-11-17 11:26:15

更新至 jQuery 1.6attr 发生了一些重大变化1.6/1.6.1。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文