gpt4 book ai didi

javascript - getAttribute() 不适用于类名?

转载 作者:行者123 更新时间:2023-12-01 01:14:07 27 4
gpt4 key购买 nike

我正在开发一个触摸屏项目,并尝试显示一些图像(将一行分成几个段落,并使用每个段落的背景来显示图像),并为所选图像添加边框(只能是一个)选择),现在我需要通过onclick()显示移动的图像,例如,首先它显示图像1-6,单击后,它应该显示图像2-7,问题是我的边框属于每个段落并且不随着图像移动,我试图在滑动移动图像之前检查是否选择了图像。

我可以使用 setattribute() 为段落设置背景图像,假设一个段落的 id 为“img5”,另一段的 id 为“img6”:
document.getElementById('img5').setAttribute("class", "noborder");
document.getElementById('img6').setAttribute("class", "borderstyle1");
然后当我尝试获取属性时: var x= document.getElementById('img1').getAttribute('class');
if(x.equals("borderStyle1"))
{.....}
但上面的内容被卡住了,它提醒我:Uncaught TypeError: x.equals is not a function,有什么建议吗?谢谢

最佳答案

您可以更改为 x == "borderStyle1"来比较文本字符串

if(x.equals("borderStyle1")){
}

var x= document.getElementById('img1').getAttribute('class');
if(x == "test1"){
alert('ok');
}
<img src="" id="img1" class="test1" />

关于javascript - getAttribute() 不适用于类名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54917523/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com