Javascript 替换整个文档JQuery中的字符
我已经尝试了很多不同的方法,但是都不起作用。现在我有:Javascript 替换整个文档JQuery中的字符,javascript,jquery,Javascript,Jquery,我已经尝试了很多不同的方法,但是都不起作用。现在我有: $(document).ready(function () { $(".page").click(function () { var html = $(document).html(); html.replace("[", "<"); alert("here"); }); }); $(文档).ready(函数(){ $(“.page”)。单击(函数(
$(document).ready(function () {
$(".page").click(function () {
var html = $(document).html();
html.replace("[", "<");
alert("here");
});
});
$(文档).ready(函数(){
$(“.page”)。单击(函数(){
var html=$(document.html();
替换(“[”,“这是您想要的吗
$(document.documentElement).html(function(i,val){
return val.replace(/\[/g,'<');
});
$(document.documentElement).html(函数(i,val){
return val.replace(/\[/g,在我看来,您正在从html创建变量,但没有将其发送回页面:
$(document).ready(function () {
//EDIT:
// $(".page").click(function () { changed to a bind type event handler to
// prevent loss of interactivity. .on() requires jQuery 1.7+ I believe.
$(".page").on('click', function () {
var htmlString = $('body').html();
// To replace ALL occurrences you probably want to use a regular expression
// htmlString.replace(/\[/g, "<");
htmlString.replace("[", "<");
//Validate the string is being manipulated
alert(htmlString);
// Overwrite the original html
$('body').html(htmlString);
});
});
$(文档).ready(函数(){
//编辑:
//$(“.page”)。单击(函数(){更改为绑定类型事件处理程序以
//防止失去交互性..on()需要jQuery 1.7+我相信。
$(“.page”)。在('click',函数(){
var htmlString=$('body').html();
//若要替换所有引用,可能需要使用正则表达式
//htmlString.replace(/\[/g,“行html.replace(“[”,”如何解释您试图实现的目标,而不是不起作用的目标?我试图用“html.replace
只更新变量html
,而不更新DOM。$(document.html)
。要设置文档的html,可以使用类似于$(document.html($(document.html()).replace(“[”,“我建议不要用新的html替换整个文档html。相反,迭代所有文本节点,并使用下面答案中的regexp在每个节点上进行替换。或者,使用现有的测试插件。有几种。时髦的语法是什么?”/[/g”。如果我说%lbrack%是那样的话,它会是return val.replace(“%lbrack%”,@user1260028/[/g
是一个正则表达式,它匹配所有的[
符号。replace(“[”,“>”)
将只替换第一个[
符号,但replace(/\[/g,”)
all[
符号。关于%lbrack%
我不能说什么,我不明白它是什么,你想要什么?@user1260028如果你想替换所有匹配项,你应该使用RegExp
s,如果你要传递字符串
,只有第一个匹配项会被替换。%lbrack%只是一个定义,它可以是12354123。基本上任何point%lbrack%碰巧它被“[”替换。非常奇怪的是,一旦我两个都被替换,该块就会消失。它只在我申请时发生“这将完成任务,但会重新创建整个DOM,失去您以前可能有的任何交互性。click函数应更改为绑定事件处理程序,如$(“.page”)。on('click',function(){});以补偿此问题。我将更新我的帖子。@user1260028如果您调用htmlString.replace([”,这将起作用。”);
而不是非转义版本
$(document).ready(function () {
//EDIT:
// $(".page").click(function () { changed to a bind type event handler to
// prevent loss of interactivity. .on() requires jQuery 1.7+ I believe.
$(".page").on('click', function () {
var htmlString = $('body').html();
// To replace ALL occurrences you probably want to use a regular expression
// htmlString.replace(/\[/g, "<");
htmlString.replace("[", "<");
//Validate the string is being manipulated
alert(htmlString);
// Overwrite the original html
$('body').html(htmlString);
});
});
var foo = html.replace(/\[/g, "<");
$(document).html(foo);