。
div {
border: 3px solid black;
}
span {
border: 2px solid blue;
}
<div contenteditable>/span>
<span contenteditable id="haha">/span><。 i>asd</i></span>
</div>/span>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
我想關注#haha,但只有當我把元素移到div之外時才會起作用
div {
border: 3px solid black;
}
span {
border: 2px solid blue;
}
<div contenteditable>/span>
</div>/span>
<span contenteditable id="haha">/span><。 i>asd</i></span>
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
目前使用這段代碼進行聚焦,是否有辦法在span元素上進行聚焦,即使它在div里面?
document. querySelector(`#haha`).focus()。
uj5u.com熱心網友回復:
試試document.getElementById("haha")(注意我去掉了'#')
Document方法getElementById()回傳一個Element物件,代表id屬性與之匹配的元素。 代表元素,其id屬性與指定的 字串。由于元素ID被要求是唯一的,如果被指定的話。 它們是快速訪問一個特定元素的有用方法。
編輯:
看起來,操縱tabindex可以讓這一做法發揮作用:
。myFunction = function() {
let x = document.getElementById("haha") 。
x.setAttribute('tabindex', '0') 。
x.focus()。
}
myFunction();
<div contenteditable>/span>
<span contenteditable="true"/span> id="haha"/span>> asd</span>/span>
</div>/span>
<button class="bold"/span>> Bold</button>
<div contenteditable="true">/span>
無論怎樣
</div>
<button class="bold"/span>> Bold</button>/span>
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
uj5u.com熱心網友回復:
嘗試設定tabindex="0"/code>。它允許那些通常不能被關注的專案得到關注
span class="hljs-selector-tag">div{
border: 3px solid black;
}
span {
border: 2px solid blue;
}
span: focus{
border-color: 綠色。
}
<div contenteditable>/span>
< span tabindex="0" contenteditable id="haha"/span>> <i>/span>asd< /i></span>
</div>/span>
<iframe name="sif4" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
uj5u.com熱心網友回復:
你需要在div中添加一些文本。
div[contenteditable]/span>{
border: 3px solid black;
}
span {
border: 2px solid blue;
}
<div contenteditable>/span>div<。 span contenteditable id="haha" > span</span></div>
<iframe name="sif5" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/331822.html
標籤:
上一篇:忽略了表的列寬
