簡體   English   中英

ParentNode返回假值

[英]ParentNode returns the false value

我在ParentNode上遇到了一些麻煩。請參閱“我正在研究所見即所得”,我想告訴我所選文本的父代是什么。 直到我有兩個具有不同樣式的文本,它才能正常工作:

<b>Bold<b> NotBold

當我單擊粗體時,它將返回BODY標記,而當我再次單擊它時,它將重新運行B標記。 NotBold相同,當我單擊Bold后單擊NotBold ,它返回B標簽,而當我再次單擊時,它返回我BODY問題出在哪里?

document.addEventListener("DOMContentLoaded",iFrameStart,false); //make iFrame Editable
function iFrameStart() {
    iframe.document.designMode="On";
}
let frame = document.getElementById('iframe');
frame.contentWindow.onselectstart=function()
{
    let frame = document.getElementById("iframe");
    let select=frame.contentWindow.getSelection(); //get the selected text
    let a = select.focusNode.parentNode; //get the parent of selected text
    //if it removed it returns the value in " "
    let array= ['ok']; //create an array
    while (a) {
        array.push(a); //add to the array
        a=a.parentNode;
    }
    console.log(array); //display the parents
};

請嘗試以下方法。 它使父節點正常。 您沒有關閉 在您的示例中標記

<iframe id='iframe'></iframe>


document.addEventListener("DOMContentLoaded", iFrameStart, false); //make iFrame Editable
function iFrameStart() {
  window.iframe.document.designMode = "On";
}
let frame = document.getElementById("iframe");
frame.contentDocument.body.innerHTML = "<b>Bold </b> <c>not bold</c>";
//window.iframe.document.getElementByTag("body")[0].innerHTML = "SAQIB";

frame.contentWindow.onclick = function() {
  let frame = document.getElementById("iframe");
  let select = frame.contentWindow.getSelection(); //get the selected text
  let a = select.focusNode.parentNode; //get the parent of selected text
  console.log(select.focusNode.parentNode);
  //if it removed it returns the value in " "
  let array = ["ok"]; //create an array
  while (a) {
    array.push(a); //add to the array
    a = a.parentNode;
  }
  console.log(array); //display the parents
};


暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM