Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript dom.style.display根本不工作_Javascript_Html_Css - Fatal编程技术网

JavaScript dom.style.display根本不工作

JavaScript dom.style.display根本不工作,javascript,html,css,Javascript,Html,Css,因此,我的HTML中有一个按钮,当我单击该按钮时,我想从页面中删除一些元素 目前,我有一个类似这样的函数,它为该按钮设置侦听器 function addListener_close(elem,i){ console.log("IN BUTTON"); elem.addEventListener("click", function(){ console.log("clicked"); //this returns 'block' which is the current displ

因此,我的HTML中有一个按钮,当我单击该按钮时,我想从页面中删除一些元素

目前,我有一个类似这样的函数,它为该按钮设置侦听器

function addListener_close(elem,i){
console.log("IN BUTTON");
elem.addEventListener("click", function(){
    console.log("clicked");
    //this returns 'block' which is the current display
    console.log(document.getElementById("newsentry-author").style.display); 
    //set the display to none
    document.getElementById("newsentry-author").style.display = "none"; 
    //this returns 'none' which is the new value
    console.log(document.getElementById("newsentry-author").style.display);
});}
但是,在我的HTML页面中,单击按钮后,元素的样式都没有改变

我哪里做错了

更新HTML代码

在美国最大的地铁系统出现冠状病毒时通勤
作者:勒布朗·詹姆斯
来源:google.com
日期:2020/03/02
每个工作日都有拥挤的火车载着500多万人,他们被恐怖威胁、轨道上的老鼠、白天的袭击和洪水干道的断裂所折磨。新冠病毒19是他们最新的担忧。病毒的传播在全国范围内引发了焦虑…
×

该函数已绑定到。使用javascript addEventListener关闭按钮

这是您的全部代码吗?因为这是一个函数,除非你调用它,否则它不会做任何事情。看起来您看到的是控制台输出,因此如何调用该函数是至关重要的,但问题中没有提到。就像这样,它工作得很好:你也可以分享你使用的HTML吗?我使用一个按钮调用函数,我将上传我的HTML代码你可以使用
,或者在JS中的按钮上附加一个侦听器。JS中的最后一行应该是这样的:
addListener\u close(document.querySelector('css-class-or-id-of-button'),即-i-value-which-that-is)当然你已经读过了,而且,毫无疑问……你需要一个“elem”变量来决定你的JS将要听的选择器。现在这还不存在。
            <div class="newsentry-clicked">
                <img class= "newsimage" src="https://dynaimage.cdn.cnn.com/cnn/digital-images/org/044409cb-e6f2-4c5b-8eaf-26078a4edef2.jpg">
                <div class="newsentry-text">
                <div class="newsentry-title">Commuting in the time of coronavirus in the nation's largest subway system</div>
                <div class="newsentry-author"><span style="font-weight: bold">Author: </span>Lebron James</div>
                <div class="newsentry-source"><span style="font-weight: bold">Source: </span>google.com</div>
                <div class="newsentry-date"><span style="font-weight: bold">Date: </span>2020/03/02</div>
                <div class="newsentry-desc-clicked">Crowded trains each weekday carry more than 5 million people hardened by terror threats and track-dwelling rats, daylight assaults and diluvial water main breaks. COVID-19 is their latest worry. The spread of the virus has unleashed anxiety across the nation'…</div>
                <a href="www.google.com">See Original Post</a>
            </div>
            <div class="close-button">×</div>