我有一個 html 頁面,其中一些單詞被分割,​所以它們看起來像??F?Rig?V?M?External?Variable,但在代碼中它們實際上是F​Rig​V​M​External​Variable​。問題是,當我單擊該單詞時,它僅選擇一個與之相鄰的部分,​而理想的行為是讓它選擇整個單詞
如何覆寫 JS 中的默認雙擊行為,使其​不視為單詞邊界?
uj5u.com熱心網友回復:
我找到了一個解決方案,它保留了單詞在特定位置被軟中斷但不干擾軟選擇的能力。是標簽</wbr>。當我替換單詞​時</wbr>,可以在</wbr>s 處軟斷,但是當我單擊單詞的任何部分時,整個單詞都會被選中。
F<wbr/>Rig<wbr/>V<wbr/>M<wbr/>External<wbr/>Variable<wbr/>
uj5u.com熱心網友回復:
您可以三次單擊以選擇整行/段落。否則,​是本問題中解釋的單詞分隔符,因此雙擊時的默認瀏覽器操作是選擇游標下的單詞。
<!DOCTYPE html>
<html lang='pt-br'>
<head>
<title>Teste</title>
</head>
<body>
a​better​test with spaces<br>
a​better​test with spaces<br>
</body>
</html>
uj5u.com熱心網友回復:
如果您不需要零寬度空格,則應該使用.replace()regex 標志洗掉它們,該標志\u使 Regex 方法能夠識別 Unicode。零寬度空間的 Unicode 是:U 200B在 JavaScript 領域是:\u200B.
function noZero(string) {
return string.replace(/\u200B/gu, '');
}
const str = document.querySelector(".zero").innerHTML;
const result = noZero(str);
document.querySelector(".clean").insertAdjacentHTML("beforeend", result);
<p class="zero">Try <u>s?e?l?e?c?t?i?n?g</u> the underlined word.</p>
<p class="clean"></p>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/521597.html
