【问题标题】:How to get updated html() when there are changes to css classes or div text contents当 css 类或 div 文本内容发生更改时如何获取更新的 html()
【发布时间】:2018-02-22 01:55:35
【问题描述】:

我对 JQuery 的 html() 函数有疑问,它不会返回 div 的更新 css。

我的 div 被封装在父 div 中。在 JQuery 中,根据一些逻辑,我通过 addClass() 或 removeClass() 在 div 上添加或删除 css 类。 我还更改了一些 div 的文本内容。

稍后我想使用 html() 函数来获取更新后的父 html() 的整个 html 并复制到其他地方。

html() function() 获取初始 html 并且没有得到 ant 更改 :(

我创建了这个主题,但描述了更新表单值的解决方案: similar problem

是否有任何类似的解决方案来更新 css 类和 div 文本内容,以便 html() 函数会对其进行更改?

【问题讨论】:

  • 我用 jQuery 3.2.1 测试过,html() 工作正常,你能提供你的演示代码吗?

标签: javascript jquery


【解决方案1】:

因为您将 html() 保存到 var,所以您调用 var 而不是使用新的。看例子

$(function(){
  var first = $('p').html();
  console.log(first); //first
  $('p').html('second');
  console.log(first); //first
  console.log($('p').html()); //second
  $('p').html('third');
  console.log(first); //first
  console.log($('p').html()); //third
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>first</p>

【讨论】:

    猜你喜欢
    • 2017-08-23
    • 1970-01-01
    • 1970-01-01
    • 2013-07-24
    • 1970-01-01
    • 1970-01-01
    • 2011-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多