admin管理员组文章数量:1391995
I am trying to translate a field error by testing if a an element have the class sv or en (swedish or english)
I tried to access the class but won't affect the javascript oute.
input.oninvalid = function(event) {
if ($("wgcurrent").hasClass("en")) {
event.target.setCustomValidity("Letters only please");
} else if ($("wgcurrent").hasClass("sv")) {
event.target.setCustomValidity("Vänligen ange endast bokstäver");
}
}
<div class="wgcurrent wg-li wg-flags flag-1 en" data-l="en" tabindex="0" aria-expanded="false" role="bobox" aria-label="Language selection: English"><a tabindex="-1" href="javascript:%20void(0);">English</a></div>
I want to make sure the input function sets the right value for the error message. What do I need to change?
I am trying to translate a field error by testing if a an element have the class sv or en (swedish or english)
I tried to access the class but won't affect the javascript oute.
input.oninvalid = function(event) {
if ($("wgcurrent").hasClass("en")) {
event.target.setCustomValidity("Letters only please");
} else if ($("wgcurrent").hasClass("sv")) {
event.target.setCustomValidity("Vänligen ange endast bokstäver");
}
}
<div class="wgcurrent wg-li wg-flags flag-1 en" data-l="en" tabindex="0" aria-expanded="false" role="bobox" aria-label="Language selection: English"><a tabindex="-1" href="javascript:%20void(0);">English</a></div>
I want to make sure the input function sets the right value for the error message. What do I need to change?
Share Improve this question edited Aug 13, 2019 at 17:30 Luvexina 84311 silver badges26 bronze badges asked Aug 13, 2019 at 17:28 9minday9minday 4034 gold badges10 silver badges22 bronze badges2 Answers
Reset to default 6As noted in the jquery docs: https://api.jquery./class-selector/
The selector for objects with a class is not
$("wgcurrent")
It's like this:
$(".wgcurrent")
The dot makes it so it selects any object with the wgcurrent class.
I believe that's the reason behind your issue, since the hasClass should work in this scenario.
However beware that this will select ALL the objects with the wgcurrent class.
Pure JavaScript look at classList
console.log(element.classList.contains('en'));//output true
本文标签: javascriptIf element has classStack Overflow
版权声明:本文标题:javascript - If element has class - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744606975a2615413.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论