jQuery 无法使用 .html() 更改 html

jQuery can't change html using .html()

本文关键字:html 更改 jQuery      更新时间:2023-09-26

我正在尝试使用jQuery更改页面中的html。虽然它可以在Firefox中工作,但它不会在chrome中工作。我该怎么做才能解决这个问题?

例如。

$('#test').html(<p>hi</p>);

将更改文本,但是如果我在文件中有其他html并用它覆盖它,则旧文本仍显示在chrome中。这可能是缓存问题吗?我该如何解决它?

你缺少引号。

$('#test').html("<p>hi</p>");

使用 ,

$('#test').html("<p>hi</p>"); //.html() accepts quoted [.html("value")] value.

我想你想附#test,所以用.append()而不是.html()

如果要覆盖内容,请使用.html().

$('#test').html("<p>hi</p>");   //will override content of test.
$('#test').append("<p>hi</p>");   //will append <p>hi</p> to test.

一些对你来说有趣的事情,

$('#test').html(value) is equivalent to $('#test').empty().append(value).

html()接受字符串。因此,您需要将这些字符封装在引号中。

$('#test').html("<p>hi</p>");

如果您想替换ID的文本 #test 则使用

$('#test').html("<p>hi</p>");

想要在测试 ID 使用中添加更多添加<p>hi</p>标记

$('#test').append("<p>hi</p>");