【问题标题】:how do i set html contents in the <body> using jquery如何使用 jquery 在 <body> 中设置 html 内容
【发布时间】:2015-04-30 23:06:06
【问题描述】:

我有:

<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 标签。所以,我想先用 class 找出这个 body 标签,然后使用 jquery 为其设置一些值。请帮帮我。

【问题讨论】:

  • 您只能设置用于用户输入的元素的值。你真正想改变什么?
  • 最后的额外关闭标签是干什么用的?
  • 您的代码中的this 是什么?如果该代码在事件处理程序中,this 将是事件的目标,而不是主体。如果要设置body的内容,为什么不写$("body").html()呢?
  • 先生,仍然没有得到 中的 html。

标签: javascript jquery html css ruby-on-rails


【解决方案1】:

这是一种更简单的方法:

$( document ).ready(function() {
    $('.wysihtml5-editor').append('<span>Your HTML content.</span>');
});

您可以使用appendprepend 插入HTML。

更多信息:

【讨论】:

【解决方案2】:
<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" );

【讨论】:

  • 我使用的文本编辑器基本上是一个 gem,它是自动生成的。我没有这方面的身份证,这就是为什么我只有班级要处理。
  • 没关系!我已经为你更新了答案。 Id 通常是最好的方法,因为您通常希望尽可能具体,但只需访问 body 就可以了。
猜你喜欢
  • 2021-12-03
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-20
  • 1970-01-01
相关资源
最近更新 更多