因此,在图像悬停时,我希望该 div 中的段落消失。所以,我正在尝试做这样的事情:
$("#One1 img").hover(function(){
var par = document.getElementById('One1 p');
par.style.display = "none";
},function(){});
但我收到错误“Uncaught TypeError: Cannot read property 'style' of null'”
我知道 div 的 getElementById 只是 var par = document.getElementById('One1');
但我怎样才能获得该 div 中的段落?谢谢!
最佳答案
document.getElementById()
只会获取具有所述 id 的元素,它不会将任何 css 选择器作为其参数。
既然你用的是jQuery,就用它提供的选择器就可以了
$("#One1 img").hover(function () {
$('#One1 p').hide();
}, function () {
$('#One1 p').show();
});
关于javascript - 使用 getElementById 设置 div 段落的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26165485/