jQuery:.closest 添加了类属性,但不是我想要的值
问题的快速预览:使用 .closest()
,我可以在目标元素的所有实例上粘贴类属性,但它不会在任何具有祖先 DOM 中的额外
。首先,这是 jQuery:
// Throw class="errorWrap" on any <li> element that contains a field that doesn't validate
highlight: function(element) {
$(element).closest("li").addClass("errorWrap");
}
(…“突出显示:”是验证插件的一部分…)
这是我的 HTML 片段:
<li>
<div>
<span class="reqField">*</span> Have you been employed with us before?<br />
<label class="radioInput"><input type="radio" name="app_prior_employ" value="Yes" /> Yes</label>
<label class="radioInput"><input type="radio" name="app_prior_employ" value="No" /> No</label>
</div>
<div>
<label for="app_prior_employ_date">If <em>yes</em>, when were you employed?</label><br />
<input type="text" name="app_prior_employ_date" value="" size="40" id="app_prior_employ_date" />
</div>
</li>
…现在,有了上面的内容,类属性 将添加到开始的
标签,但它的值将是空而不是errorWrap正如我在这个问题的顶部提到的,此问题仅发生在混合中有
元素的实例上(正如它们在上面所做的那样)。此问题不会发生在
未到位的其他实例上,例如:<li>
<span class="reqField">*</span> Can you travel if a job requires it?<br />
<label class="radioInput"><input type="radio" name="app_travel_for_job" value="Yes" /> Yes</label>
<label class="radioInput"><input type="radio" name="app_travel_for_job" value="No" /> No</label>
</li>
我无法弄清楚为什么 jQuery 会成功添加该类属性在我想要的任何地方,但无法仅在 DOM 中具有额外元素的那些元素中插入我定义的值。我使用 .closest()
是否不正确?
稍后添加:这是问题的标记屏幕截图:https:/ /i.sstatic.net/aiiaX.png
Quick preview of the problem: using .closest()
, I'm able to stick a class attribute on all instances of the target element, but it won't insert the class value on any that have an extra <div>
in the ancestor DOM.
First, here's the jQuery:
// Throw class="errorWrap" on any <li> element that contains a field that doesn't validate
highlight: function(element) {
$(element).closest("li").addClass("errorWrap");
}
(…The 'highlight:' is part of the Validate plug-in…)
And here's a snippet of my HTML:
<li>
<div>
<span class="reqField">*</span> Have you been employed with us before?<br />
<label class="radioInput"><input type="radio" name="app_prior_employ" value="Yes" /> Yes</label>
<label class="radioInput"><input type="radio" name="app_prior_employ" value="No" /> No</label>
</div>
<div>
<label for="app_prior_employ_date">If <em>yes</em>, when were you employed?</label><br />
<input type="text" name="app_prior_employ_date" value="" size="40" id="app_prior_employ_date" />
</div>
</li>
…now, with the above, the class attribute will be added to the opening <li>
tag when it's supposed to, but it's value will be empty rather than errorWrap
As I mentioned at the top of this question, this issue occurs on only instances that have the <div>
elements in the mix (as they do immediately above). This issue does not occur on other instances where the <div>
is not in place, such as:
<li>
<span class="reqField">*</span> Can you travel if a job requires it?<br />
<label class="radioInput"><input type="radio" name="app_travel_for_job" value="Yes" /> Yes</label>
<label class="radioInput"><input type="radio" name="app_travel_for_job" value="No" /> No</label>
</li>
I can't figure out why jQuery would succeed in adding the class attribute everywhere I want it to, but fail to insert the value I've defined only in those that have an extra element in the DOM. Am I using .closest()
improperly?
Added moments later: Here's a marked-up screen shot of the issue: https://i.sstatic.net/aiiaX.png
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正确使用了closest(),但在该实例中highlight()函数的上下文可能是错误的。在脚本检查器中,在突出显示函数中放置一个断点,然后查看 element 是否实际上是您认为的输入。
或者,您说 li 有一个类,但当 div 处于混合状态时没有任何值。也许类正在设置然后立即取消设置。使用脚本检查器并设置断点将帮助您解决这个问题。
You're using closest() properly, but maybe the context in which the highlight() function is wrong in that instance. In the scripts inspector, put a breakpoint inside the highlight function, and see if element is actually the input you think it is.
Alternatively, you say the li has a class, but no value when the div is in the mix. Maybe the class is getting set then immediately unset. Using the script inspector and setting breakpoints will help you figure that out.
使用父母代替
Use parents instead