Javascript 突变观察者寻址 <li> 中的一个元素
Javascript mutation observer adressing an element within a <li>
我正在使用变异观察器来观察 <ul>
元素发生的变化。观察时,我得到以下输出:
<li class="rt__body-row full flex gc-07 rt__body-row--odd">
<span class="rt__row-left short">48,359.50</span>
<span class="rt__row-center">0.063</span>
<span class="rt__row-right">01:57:44</span>
</li>
现在我想写一个关于第二个 span 文本 (0.063) 的 if 语句。如何处理 javascript 中的第二个跨度?
我对编程很陌生,下面是我的代码:
function obser1() {
const trades = document.querySelector('.scrollbar-dark');
const observer = new MutationObserver(function (mutations) {
mutations.forEach(function (mutation) {
if (mutation.addedNodes.length) {
console.log(mutation.addedNodes[0]);
}
})
});
console.log("obs1");
observer.observe(trades, {
childList: true,
subtree: true,
attributes: true,
characterData: true
})
console.log("observed");
}
window.setTimeout(obser1, 1700);
下面是我的清单文件:
"name": "Bybit orderbook color",
"version": "1.1",
"manifest_version": 2,
"content_scripts": [
{
"matches": [ "https://www.bybit.com/trade/usdt/BTCUSDT" ],
"css": [ "Style.css" ],
"run_at": "document_idle",
"js": [ "background.js" ]
}
],
"permissions": [ "activeTab" ]
}
假设 mutation.addedNodes[0] 是一个 html 集合,您可以
const vals = [...mutation.addedNodes[0].querySelectorAll("span")].map(span => span.textContent);
如果一个字符串你可以做
const fragment = document.createElement("div");
fragment.innerHMTL = mutation.addedNodes[0];
const vals = [...fragment.querySelectorAll("span")].map(span => span.textContent);
我正在使用变异观察器来观察 <ul>
元素发生的变化。观察时,我得到以下输出:
<li class="rt__body-row full flex gc-07 rt__body-row--odd">
<span class="rt__row-left short">48,359.50</span>
<span class="rt__row-center">0.063</span>
<span class="rt__row-right">01:57:44</span>
</li>
现在我想写一个关于第二个 span 文本 (0.063) 的 if 语句。如何处理 javascript 中的第二个跨度? 我对编程很陌生,下面是我的代码:
function obser1() {
const trades = document.querySelector('.scrollbar-dark');
const observer = new MutationObserver(function (mutations) {
mutations.forEach(function (mutation) {
if (mutation.addedNodes.length) {
console.log(mutation.addedNodes[0]);
}
})
});
console.log("obs1");
observer.observe(trades, {
childList: true,
subtree: true,
attributes: true,
characterData: true
})
console.log("observed");
}
window.setTimeout(obser1, 1700);
下面是我的清单文件:
"name": "Bybit orderbook color",
"version": "1.1",
"manifest_version": 2,
"content_scripts": [
{
"matches": [ "https://www.bybit.com/trade/usdt/BTCUSDT" ],
"css": [ "Style.css" ],
"run_at": "document_idle",
"js": [ "background.js" ]
}
],
"permissions": [ "activeTab" ]
}
假设 mutation.addedNodes[0] 是一个 html 集合,您可以
const vals = [...mutation.addedNodes[0].querySelectorAll("span")].map(span => span.textContent);
如果一个字符串你可以做
const fragment = document.createElement("div");
fragment.innerHMTL = mutation.addedNodes[0];
const vals = [...fragment.querySelectorAll("span")].map(span => span.textContent);