![](/img/trans.png)
[英]Find and replace nth occurrence of [bracketed] expression in string
[英]Find and replace occurrence of string in whole html itself
假设
<html>
<head>....</head>
<body>
.
. // Occurrences are here
.
</body>
</html>
我做
$(function()
{
$('html').html() = $('html').html().replace(/strToFind/g,'somethingElse');
});
在脑海中,但它不起作用。 如何查找和替换html文档本身中所有出现的字符串(而不存储在变量中)?
谢谢
.html()
是一个返回HTML的函数,您无法为其分配。 如果要在jQuery中更改对象的HTML,请将新的HTML作为函数的参数:
$('html').html($('html').html().replace(/strToFind/g,'somethingElse'));
$('html').html()
将返回html字符串,但不会设置它。
您可以使用此功能
function rep_all()
{
var str = $('html').html();
str.replace('strtofind','strtoreplace');
$('html').html(str);
}
.html()
是写在html页面上的东西,它不像变量,因此您不能为其分配任何thig。 您需要将值放在其中。像这样:
$('html').html( $('html').html().replace(/strToFind/g,'somethingElse'));
看到这里: http : //api.jquery.com/html/
有人回答说.html()
是不可分配的,您必须将替换后的内容作为参数传递。
只是指出,替换整个页面的html
内容非常少见。
当您定义要替换的内容在body
内部时,您至少(仍然不好)应仅针对body
:
$('body').html($('body').html().replace(/strToFind/g,'somethingElse'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.