发布于2021-06-16 12:39 阅读(1246) 评论(0) 点赞(6) 收藏(1)
我正在添加这段代码以将 an 附加ID
到元素li.search
内的类 ( ) li
。
document.querySelector('li.search').id = 'ControlOverlay';
它适用于我的一些网站,但最近安装的,它不会附加ID
到class
。
当我在 上测试时,我的实现有效w3schools.com
,但在我的网站上无效...
关于什么可能是错误的任何想法?
我忘记引用任何图书馆了吗?
这是我的实现:
<!DOCTYPE html>
<html>
<style>
#overlays {position:fixed;top:0;left:0;right:0;bottom:0;z-index:0;background:rgba(110, 173, 238,.9);}
.standby2 {display:none;}
ul {list-style:none;}
</style>
<body>
<ul class="menu">
<li class="search"><a aria-label="Open" href="#"><span class="gp-icon icon-search"><svg viewBox="0 0 512 512" aria-hidden="true" role="img" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="1em" height="1em">
<path fill-rule="evenodd" clip-rule="evenodd" d="M208 48c-88.366 0-160 71.634-160 160s71.634 160 160 160 160-71.634 160-160S296.366 48 208 48zM0 208C0 93.125 93.125 0 208 0s208 93.125 208 208c0 48.741-16.765 93.566-44.843 129.024l133.826 134.018c9.366 9.379 9.355 24.575-.025 33.941-9.379 9.366-24.575 9.355-33.941-.025L337.238 370.987C301.747 399.167 256.839 416 208 416 93.125 416 0 322.875 0 208z"></path>
</svg><svg viewBox="0 0 512 512" aria-hidden="true" role="img" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="1em" height="1em">
<path d="M71.029 71.029c9.373-9.372 24.569-9.372 33.942 0L256 222.059l151.029-151.03c9.373-9.372 24.569-9.372 33.942 0 9.372 9.373 9.372 24.569 0 33.942L289.941 256l151.03 151.029c9.372 9.373 9.372 24.569 0 33.942-9.373 9.372-24.569 9.372-33.942 0L256 289.941l-151.029 151.03c-9.373 9.372-24.569 9.372-33.942 0-9.372-9.373-9.372-24.569 0-33.942L222.059 256 71.029 104.971c-9.372-9.373-9.372-24.569 0-33.942z"></path>
</svg></span></a></li> </ul>
<script>
document.querySelector('li.search').id = 'ControlOverlay';
document.getElementById("ControlOverlay").setAttribute("onclick", "ShowOverlay();");
function ShowOverlay(){document.getElementById("overlays").classList.toggle("standby2")}
</script>
这是一个jsfiddle
问题出在 document.getElementById("overlay")
您overlay
的 HTML 中没有带有 id 的元素。
我也会编辑
document.getElementById("ControlSearchOverlay").setAttribute("onclick", "ShowHideOverlay();");
function ShowHideOverlay(){
document.getElementById("overlay").classList.toggle("standby")
}
至:
document.getElementById("ControlSearchOverlay").onclick = function() {
document.getElementById("overlay").classList.toggle("standby")
}
正如 user4642212 提到的,最好使用 addEventListener https://developer.mozilla.org/de/docs/Web/API/EventTarget/addEventListener
document.getElementById("ControlSearchOverlay").addEventListener('click', function() {
document.getElementById("overlay").classList.toggle("standby")
})
作者:黑洞官方问答小能手
链接:http://www.qianduanheidong.com/blog/article/130777/d22c8238f15249f4b71b/
来源:前端黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 前端黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-3
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!