addClass函数不起作用

时间:2016-01-15 23:32:51

标签: jquery

我想将effect类添加到my input type="submit"已有的任何现有类中,只要该操作包含字符串special即可。

我正在使用以下代码:

<body>

<form action="/go/special" method="POST" target="_blank">
<input type="submit" class="zzz" value="click me"></form>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

<script>
jQuery(function($) {
    // find all forms that have "special" in the action, find their input, and add the class
    $('form[action*="special"] input[type="submit"]').addClass('effect');
});
</script>
</body>

如果它能正常工作,输出应该是:

<form action="/go/special" method="POST" target="_blank">
<input type="submit" class="zzz effect" value="click me"></form>

但它仍然存在:

<form action="/go/special" method="POST" target="_blank">
<input type="submit" class="zzz" value="click me"></form>

1 个答案:

答案 0 :(得分:0)

这个脚本对我来说正常。您的测试环境可能会出现一些奇怪的现象,并且为了消除一些可能的混淆元素,我会尝试在$( document ).ready(function() {}调用中包装您的脚本。

不,这不应该是必要的,而其他人可以自由地使用风格提示,但我唯一一次遇到这样的错误,这是由于某种原因发生故障。

相关问题