我正在尝试使用以下函数替换 DOM 树的每个 textNode
:
//Replace each word objective with reposition in each control of the actual jQuery object
jQuery.fn.replaceEachOne = function (objective, reposition) {
var regExp = new RegExp('([\\s]'+objective+'[\\s])', "igm");
this.contents().each(function(){
if (this.nodeType == 3) {//if is a Node.TEXT_NODE, IE don't have Node object
//console.log("pName: "+this.parentNode.nodeName+" pType: "+this.parentNode.nodeType+" Data: " + this.parentNode.data);
if(this.data.search(regExp) != -1){
var temp = document.createElement("span");
temp.innerHTML = this.data.replace(regExp, reposition);
//Insert the new one
this.parentNode.insertBefore(temp, this);
// Remove original text-node:
this.parentNode.removeChild(this);
}
}
else{
$(this).replaceEachOne(objective, reposition);
}
});
}
它可以工作,但会抛出 20 个这样的错误(Google Chrome,IE 不会抛出):
Unsafe JavaScript attempt to access frame with URL http://cdn.apture.com/media/html/aptureLoadIframe.html?v=21872561 from frame with URL http://c-jfmunoz:5000/SitePages/Home.aspx. Domains, protocols and ports must match.
进行一些调试后,我发现它在将文本节点插入 Web 表单时抛出异常。
我必须将此 JavaScript 附加到 Sharepoint 2010 网站。在本地查看时,Chrome 不会抛出异常。
我该如何解决这个问题?
最佳答案
你无法回避它不想做 iframe 的事实,这就是它的方式,但你可以通过替换来避免错误
this.contents().each(function(){
与
this.contents().not('iframe').each(function(){
关于javascript - 替换文本节点时,不安全的 JavaScript 尝试访问 Chrome 中的框架错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4817599/