Assume
<html>
<head>....</head>
<body>
.
. // Occurrences are here
.
</body>
</html>
I do
$(function()
{
$('html').html() = $('html').html().replace(/strToFind/g,'somethingElse');
});
in head, but it does't work. How I do to find and replace all occurrence of string in html document itself (not store in variable)?
Thanks
.html()
is a function that returns the HTML, you can't assign to it. If you want to change the HTML of an object in jQuery, you put the new HTML as a parameter to the function:
$('html').html($('html').html().replace(/strToFind/g,'somethingElse'));
$('html').html()
would return the html string , but won't set it.
You can use this function
function rep_all()
{
var str = $('html').html();
str.replace('strtofind','strtoreplace');
$('html').html(str);
}
.html()
is something which writes on the html page it is not like variable so you can't assign anythig to it. you need to put the value inside it.like this:
$('html').html( $('html').html().replace(/strToFind/g,'somethingElse'));
see here: http://api.jquery.com/html/
It's been answered that the .html()
is not assignable, you would have to pass the replaced content as a parameter.
Just to point out, it is very uncommon to replace the whole page's html
content.
As you defined that the contents to be replaced are inside the body
, you should at least (still not good) target only the body
:
$('body').html($('body').html().replace(/strToFind/g,'somethingElse'))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.