Javascript IE8:“;ln.innerHTML为null或不是对象;
我正在使用nicedit.js所见即所得库,但插入链接的按钮有问题 单击按钮将弹出一个面板,在其中输入url和标题,然后单击提交:链接应插入光标位置。但我在IE8中发现以下错误: ln.innerHTML为null或不是对象Javascript IE8:“;ln.innerHTML为null或不是对象;,javascript,internet-explorer-8,nicedit,Javascript,Internet Explorer 8,Nicedit,我正在使用nicedit.js所见即所得库,但插入链接的按钮有问题 单击按钮将弹出一个面板,在其中输入url和标题,然后单击提交:链接应插入光标位置。但我在IE8中发现以下错误: ln.innerHTML为null或不是对象 错误行是: if (this.ln.innerHTML == tmp) { 完整代码 var nicLinkButton = nicEditorAdvancedButton.extend({ addPane : function() { this.ln
错误行是:
if (this.ln.innerHTML == tmp) {
完整代码
var nicLinkButton = nicEditorAdvancedButton.extend({
addPane : function() {
this.ln = this.ne.selectedInstance.selElm().parentTag('A');
this.addForm({
'' : {type : 'title', txt : 'Add/Edit Link'},
'href' : {type : 'text', txt : 'URL', value : 'http://', style : {width: '150px'}},
'title' : {type : 'text', txt : 'Title'},
'target' : {type : 'select', txt : 'Open In', options : {'' : 'Current Window', '_blank' : 'New Window'},style : {width : '100px'}}
},this.ln);
},
submit : function(e) {
var url = this.inputs['href'].value;
if(url == "http://" || url == "") {
alert("You must enter a URL to Create a Link");
return false;
}
this.removePane();
if(!this.ln) {
var tmp = 'javascript:nicTemp();';
this.ne.nicCommand("createlink",tmp);
this.ln = this.findElm('A','href',tmp);
// set the link text to the title or the url if there is no text selected
if (this.ln.innerHTML == tmp) {
this.ln.innerHTML = this.inputs['title'].value || url;
};
}
if(this.ln) {
var oldTitle = this.ln.title;
this.ln.setAttributes({
href : this.inputs['href'].value,
title : this.inputs['title'].value,
target : this.inputs['target'].options[this.inputs['target'].selectedIndex].value
});
// set the link text to the title or the url if the old text was the old title
if (this.ln.innerHTML == oldTitle) {
this.ln.innerHTML = this.inputs['title'].value || this.inputs['href'].value;
};
}
}
}))
即使是nicedit.com主页上的示例也没有插入链接,尽管它也没有显示此错误。Internet Explorer处理innerHTML的方式与其他浏览器不同,尤其是在讨论表元素时。请参阅以进行讨论
根据您的具体情况,有各种各样的修复方法-包括大多数情况。或者,如果希望与IE兼容,您可能需要删除并重新创建表元素,而不是更新其内部HTML。一些简单的防御逻辑应该可以工作:
if (!this.ln.innerHTML || this.ln.innerHTML == tmp) {
this.ln.innerHTML = this.inputs['title'].value || url;
};
如果未选择任何文本,则会出现此问题 在没有选择任何文本的情况下,我对nicEdit的技巧是将通过addlink表单给出的标题粘贴到文档中并选择它,然后其余代码的工作方式与选择文本时的工作方式相同 我使用了下面链接中描述的函数pasteHtmlAtCaret来粘贴标题 在contenteditable div中插入插入符号处的html
var selected = getSelected();
if (selected == '')
pasteHtmlAtCaret(this.inputs['title'].value,true);
if(!this.ln) {
var tmp = 'javascript:nicTemp();';
this.ne.nicCommand("createlink",tmp);
this.ln = this.findElm('A','href',tmp);
// set the link text to the title or the url if there is no text selected
if (!this.ln.innerHTML || this.ln.innerHTML == tmp) {
this.ln.innerHTML = this.inputs['title'].value || url;
};
}
getSelected也是一个简单的函数,如下所示
function getSelected()
{
if (document.selection)
return document.selection.createRange().text;
else
return window.getSelection();
}