0

nicedit.js wysiwygライブラリを使用していますが、リンクを挿入するボタンに問題があります。

ボタンをクリックすると、URLとタイトルを入力するパネルが表示され、[送信]をクリックします。リンクはカーソル位置に挿入されます。しかし、IE8では次のエラーが発生します。

ln.innerHTMLがnullであるか、オブジェクトではない


エラー行は次のとおりです。

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ホームページの例でもリンクは挿入されていませんが、このエラーも表示されていません。

4

3 に答える 3

0

この問題は、テキストが選択されていないときに発生します

テキストが選択されていない状況でのnicEditの私のトリックは、リンクの追加フォームを介して指定されたタイトルをドキュメントに貼り付けて選択することです。残りのコードは、テキストが選択されたときに機能します。

以下のリンクに記載されている関数 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();
}
于 2013-08-24T18:32:07.960 に答える
0

Internet Explorer は、特にテーブル要素について話している場合、他のブラウザーと同じ方法で innerHTML を処理しません。ディスカッションについては、こちらを参照してください。

特定の状況に応じて、あらゆる種類の修正があります。この提案はほとんどの場合をカバーしています。または、IE と互換性を持たせたい場合は、innerHTML を更新するのではなく、テーブル要素を削除して再作成する必要がある場合があります。

于 2012-03-22T21:41:57.017 に答える
0

いくつかの単純な防御ロジックが機能するはずです。

    if (!this.ln.innerHTML || this.ln.innerHTML == tmp) {
        this.ln.innerHTML = this.inputs['title'].value || url;
    };
于 2012-03-22T21:42:00.313 に答える