如何使用jQuery设置<body>中的HTML内容

3

我有:

<body marginwidth="0" marginheight="0" contenteditable="true" 
    class="bootsy required form-control bootsy_text_area wysihtml5-editor" 
    spellcheck="true" 
    style="color: rgb(85, 85, 85); cursor: auto; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; line-height: 20px; letter-spacing: normal; text-align: start; text-decoration: none; text-indent: 0px; text-rendering: auto; word-break: normal; word-wrap: break-word; word-spacing: 0px; background-color: rgb(255, 255, 255);">
</body>

我使用了以下代码,但是它没有起作用:
if ($("body").hasClass("wysihtml5-editor")) {
     $("body").html('this is a test content.');
});

我在我的Rails应用程序中使用Bootsy,它会在浏览器的源代码中生成此body标记。因此,我想首先使用jQuery查找具有类的此body标记,然后设置一些值。请帮帮我。


你只能设置用于用户输入的元素的值。你实际上想要改变什么? - Barmar
非常末尾的额外关闭标签是用来做什么的? - Hunter Frazier
你的代码中的 this 是什么?如果该代码位于事件处理程序中,则 this 将是事件的目标,而不是主体。如果您想设置主体内容,为什么不写 $("body").html() 呢? - Barmar
先生,仍然无法在<body>中获取HTML。 - Uday kumar das
2个回答

0
<div value="" marginwidth="0" id="bodyId"  marginheight="0" contenteditable="true" 
    class="bootsy required form-control bootsy_text_area wysihtml5-editor" 
    spellcheck="true" 
    style="color: rgb(85, 85, 85); cursor: auto; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; line-height: 20px; letter-spacing: normal; text-align: start; text-decoration: none; text-indent: 0px; text-rendering: auto; word-break: normal; word-wrap: break-word; word-spacing: 0px; background-color: rgb(255, 255, 255);">zxcv</div>

这里是Javascript代码!别忘了引入Jquery库!https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.js

这里是JS代码!

    $("body").replaceWith("thing");
    $( "body" ).attr( "value", "Beijing Brush Seller" );

我使用的文本编辑器基本上是一个宝石,是自动生成的。我没有这个的ID,所以我只能处理类。 - Uday kumar das
没问题!我已经为您更新了答案。通常情况下,ID是最好的选择,因为您通常希望尽可能具体,但仅访问正文也可以。 - Urasquirrel

0

@Udaykumardas 也许你应该触发一个事件,然后再改变内容?参见答案:https://dev59.com/wHI-5IYBdhLWcg3wPF12 - Edmundo Santos

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接