JSBIN替換文本的元素,它應該工作您不會將替換的文本添加爲頁面的html。
看到,當你使用字符串對象的「替換」功能時,你正在操作文本,就是這樣,文本,replace function返回一個帶有操縱文本的新字符串,你需要將該文本插入到html中不知何故。
即如果更換在下面的文本富:
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.
什麼是'mytext'? - 你有沒有重新追加到DOM?你有沒有檢查過,以確認這些跨度是否在那裏? – tymeJV 2014-10-28 21:13:34
您可以使用jsFiddle.net示例或堆棧片段重新創建問題嗎? – j08691 2014-10-28 21:14:27
在使用'replace()'之後你是否實際插入了html?顯示更多代碼 – charlietfl 2014-10-28 21:15:50