gpt4 book ai didi

javascript - 在 JavaScript 中比较类名和变量

转载 作者:行者123 更新时间:2023-11-30 09:52:17 24 4
gpt4 key购买 nike

我的输入元素的类名如下

<input class="so os us">

我想做的是如果一个变量匹配一个特定的类名然后做一些事情

$(function(){
var x="os";
var y=$("input").attr("class");
if(x=y){
//since the class name contains the word "os" it has to perform the actions mentioned here
}
});

我不知道如何实现这一目标。

最佳答案

你可以使用.hasClass():

$(function() {
var x = "os";
var y = $("input").hasClass(x);
if (y) {
$('input').val('foobar!!!').css('color', 'red');
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="so os us">

关于javascript - 在 JavaScript 中比较类名和变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35866938/

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