Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/445.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript replace()添加的元素不是';即使使用.on()也无法单击_Javascript_Jquery_Events_Jquery Events - Fatal编程技术网

Javascript replace()添加的元素不是';即使使用.on()也无法单击

Javascript replace()添加的元素不是';即使使用.on()也无法单击,javascript,jquery,events,jquery-events,Javascript,Jquery,Events,Jquery Events,我正在尝试使用.replace创建一些跨度,以便稍后单击: mytext.replace("someword","<span class='clickme'>someword</span>") 但这也不起作用,感觉我错过了什么 如果将替换的文本重新应用到从中采样文本的元素,则应该可以使用:) var mytext=$('p').text(), changes=mytext.replace(“someword”、“someword”); $('p').html(更改);

我正在尝试使用
.replace
创建一些跨度,以便稍后单击:

mytext.replace("someword","<span class='clickme'>someword</span>")

但这也不起作用,感觉我错过了什么

如果将替换的文本重新应用到从中采样文本的元素,则应该可以使用:)

var mytext=$('p').text(),
changes=mytext.replace(“someword”、“someword”);
$('p').html(更改);
$(文档)。在(“单击”,“单击我”,函数()上){
警惕(“喵喵”);
})
。单击我{
背景:#f00;
颜色:#fff;
光标:指针;
}

Lorem ipsum dolor someword坐在那里

JSBIN

发生的情况是,您没有将替换的文本作为html添加到页面中

你看,当你使用string对象的“replace”函数时,你在处理文本,就是文本,返回一个新的字符串和被处理的文本,你需要以某种方式将该文本插入html

i、 e.如果在以下文本中替换foo:

var justText = "Hi I'm foo"; // Just some text
// First the replace function returns a new string, it does not modify the original string, so you have to reassign the value
justText = justText.replace("foo", "<span>foo</span>"); // replace will return "Hi I'm <span>foo</span>", but you need to assign it.
// Assign the text to a paragraph
var p = document.getElementById("paragraph");
// Then because you want to insert new HTML DOM elements into an existing element
// you have to use either the "innerHTML" property of HTML elements, or the "html" jQuery function (which use innerHTML internaly);
p.innerHTML = justText; // Once you have inserted the DOM elements, the click bindings will be attached.
var justText=“你好,我是foo”//只是一些文字
//首先,replace函数返回一个新字符串,它不会修改原始字符串,因此必须重新赋值
justText=justText.replace(“foo”,“foo”);//replace将返回“Hi I'm foo”,但您需要分配它。
//将文本指定给段落
var p=document.getElementById(“段落”);
//然后,因为您希望将新的HTMLDOM元素插入到现有元素中
//您必须使用HTML元素的“innerHTML”属性或“HTML”jQuery函数(内部使用innerHTML);
p、 innerHTML=justText;//插入DOM元素后,将附加click绑定。

什么是
mytext
?-您是否曾经重新附加到DOM?您是否已检查以验证这些跨距是否确实存在?是否可以使用JSFIDLE.net示例或堆栈片段重新创建问题?在使用
replace()
之后,是否确实插入了html?显示更多代码以您的问题为借口出现问题,因为您显示的
.on()
的委托形式对于动态创建的内容非常有效。我的猜测是,您实际上并没有像您想象的那样修改DOM。如果需要进一步帮助,请显示该代码的其余部分。更好的是,在JSFIDLE中重现您的简单问题。。。
var justText = "Hi I'm foo"; // Just some text
// First the replace function returns a new string, it does not modify the original string, so you have to reassign the value
justText = justText.replace("foo", "<span>foo</span>"); // replace will return "Hi I'm <span>foo</span>", but you need to assign it.
// Assign the text to a paragraph
var p = document.getElementById("paragraph");
// Then because you want to insert new HTML DOM elements into an existing element
// you have to use either the "innerHTML" property of HTML elements, or the "html" jQuery function (which use innerHTML internaly);
p.innerHTML = justText; // Once you have inserted the DOM elements, the click bindings will be attached.