我有一個導航文本,我會選擇 * 之后的最后一句
例子;
Home*Development*Mobil and Web Development
我只會選擇 * --> (Mobil and Web Development) 之后的最后一句
uj5u.com熱心網友回復:
如果Mobil and Web Development要從文本中進行選擇,可以使用.split(/[*] /).pop().
演示
顯示代碼片段
var n = "Home*Development*Mobil and Web Development".split(/[*] /).pop();
console.log(n)
uj5u.com熱心網友回復:
您可以為此使用 Javascript正則運算式
let chain = "Home*Development*Mobil and Web Development";
let pattern = /.*\*(.*)$/
let matches = chain.match(pattern);
console.log(matches[1]);
uj5u.com熱心網友回復:
假設您想要做的是在最后的“句子”上設定一些樣式,您可以在啟動時運行一些 JS 以查找具有特定類的所有元素,將最后一部分分離出來并將其包裝在一個跨度中并選擇它:
const selectLasts = document.querySelectorAll('.selectLast');
selectLasts.forEach(selectLast => {
//note there are 'neater' ways of doing this but each step is deliberately spelled out here to show what is going on
const text = selectLast.innerHTML;
const arr = text.split('*');
const lastText = arr.pop();
selectLast.innerHTML = arr.join('*');
const lastEl = document.createElement('span');
lastEl.innerHTML = '*' lastText;
selectLast.appendChild(lastEl);
});
.selectLast span {
background-color: yellow;
}
<div class="selectLast">Home*Development*Mobil and Web Development</div>
uj5u.com熱心網友回復:
您可以像這樣在 div 中插入一個子元素,然后使用 CSS 進行選擇。
div > span {
color:red;
}
<div>
Home "" Development "" <span>Mobil and Web Development</span>
</div>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/336950.html
標籤:javascript 查询 css
