javascript - 如果变量得到删除类
问题描述
如果有变量 get (SID) 则需要删除现有的类 (forceview)
网址示例:https ://example.com/?SID=eqwe7q7e6868
我正在使用变量并尝试插入显示(无)。
function sid() {
// Gets a url that includes the `?` character
var url = "somedomain.net?search";
if (url.indexOf("?") > 0) {
// Selects elements with the "forceview" class
const forceviews = document.getElementsByClassName("forceview");
// Loops through these elements and removes them from display
for (let forceview of forceviews){
forceview.style.display = "none";
}
console.log("all `.forceview` elements have been hidden");
}
}
sid();
span.old-price.sly-old-price.no-display.forceview {
display: inline !important;
}
<span class="old-price sly-old-price no-display forceview">
<span class="price-container price-final_price tax weee">
<span class="price-label">Preço Normal
</span>
<span id="old-price-1263-final_price" data-price-amount="19.9" data-price-type="oldPrice" class="price-wrapper ">
<span class="price">R$19,90</span>
</span>
</span>
</span>
代码没有返回错误。
解决方案
document.getElementsByClassName() 不接受 css 选择器。你应该写
document.getElementsByClassName("forceview")[0]
[0] 因为 document.getElementsByClassName 返回一个元素数组。
推荐阅读
- javascript - 用字符串替换和删除数组中的字符
- c++ - 寻找指针忽略指令时为空
- c# - mvvm cross 部分声明不能指定不同的基类
- java - 是否有一个关键字可以代表Java中字母表中的所有字母
- windows - Kubernetes exec return the mess from container
- c# - C#: How to Rotate Left the Bits of a Byte?
- python - 收到 5^2 的错误结果
- python - Azure SQL database connection with pyodbc
- windows - 使用管理员权限远程启动客户端应用程序
- python - 从 Python 中的值列表计算累积分布函数