Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/432.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函数无法将img/图像源的样式显示从无更改为块_Javascript_Html - Fatal编程技术网

Javascript函数无法将img/图像源的样式显示从无更改为块

Javascript函数无法将img/图像源的样式显示从无更改为块,javascript,html,Javascript,Html,下面是一个简单的代码 小提琴: 这对我来说已经两天没用了 在我的网站中,第二个,即 旗 图像样式显示更改工作, 但是第一个,即 向上投票 图像样式显示更改不起作用。 但是在Fiddle中,两个样式显示更改功能都不起作用 HTML: 我以为这是JavaScript的基本功能,不知道还能做些什么来解决这个问题 啊,当简单的事情不起作用的时候 编辑: 似乎另一个div的填充区域覆盖了其中一个图像(几乎完全~95%),我无法单击该图像,而只是单击另一个div的填充区域。一切都安排好了。在这里学了一两件事

下面是一个简单的代码 小提琴: 这对我来说已经两天没用了

在我的网站中,第二个,即

图像样式显示更改工作, 但是第一个,即

向上投票

图像样式显示更改不起作用。 但是在Fiddle中,两个样式显示更改功能都不起作用

HTML:

我以为这是JavaScript的基本功能,不知道还能做些什么来解决这个问题

啊,当简单的事情不起作用的时候

编辑:
似乎另一个div的填充区域覆盖了其中一个图像(几乎完全~95%),我无法单击该图像,而只是单击另一个div的填充区域。一切都安排好了。在这里学了一两件事。谢谢大家。

当简单的事情不起作用时,试着用简单的解决方案来解决它。:)尝试更改
setAttribute
部分开头的代码;说change
document.getElementById(“upgrey”).setAttribute(“style”,“display:none”)
to
document.getElementById(“upgrey”).style=“display:none”等等。

我的朋友,你的代码非常完美,而且正在运行

请参见此示例:

请参阅此讨论,它是一个JSFIDLE配置。当您使用外部文件时

我用你的小提琴做了这件事,现在它起作用了

如果你想让它像文档一样使用它。准备好了吗


在正确的位置加载JavaScript时工作正常:@NewToJS-他的所有ID都是唯一的。您指的是什么?更易于使用的属性:
document.getElementById(“upgrey”).style.display=“none”
。不要使用块来显示,使用
(空字符串),这样图像将采用其默认样式或其他继承样式(可能不是块)。查看设置选项的左上角区域,在Fiddle选项上方。确定了。。从“onLoad”到“No wrap-in”,您的意思是:
…style.display=“none”
;-)@j08691:这是一条出路吗?这就是我使用@RobG的原因。它的工作原理是一样的,实际上,您的修复根本不起作用。该属性返回一个表示元素的样式属性的对象。这个解决方案是错误的。好的@Tiny Giant,也许你可以强调一下使用
document.getElementById(elementid.style=“display:none”有什么区别
而不是
document.getElementById(elementid).style.display=“无”<img id='upgrey' class='up' src='http://s24.postimg.org/8v77coekx/up_grey.png' width='26px' onclick='upvote();'>
<img id='upgold' class='up' src='http://s24.postimg.org/jtdvh4dsh/up_raised.png' width='26px' style='display:none'>
<img id='flaggrey' class='flag' src='http://s1.postimg.org/egcymr8cb/flag_grey.png' height='26px' onclick='flag();'>
<img id='flagred' class='flag' src='http://s1.postimg.org/a5yar6397/flag_raised.png' height='26px' style='display:none'>
function upvote() {
    document.getElementById("upgrey").setAttribute("style", "display:none");
    document.getElementById("upgold").setAttribute("style", "display:block");
}
function flag() {
    document.getElementById("flaggrey").setAttribute("style", "display:none");
    document.getElementById("flagred").setAttribute("style", "display:block");
}
(function() {
   // your page initialization code here
   // the DOM will be available here

})();