[英]Select HTML elements with jQuery by using logical OR in the selector
At the moment I have this HTML structure: 目前,我有这个HTML结构:
...
<div data-is_array="t" data-simpletype="int">
<input type="text">
</div>
<div data-is_array="t" data-simpletype="char">
<input type="text">
</div>
<div data-is_array="t" data-simpletype="text">
<textarea></textarea>
</div>
<div data-is_array="t" data-simpletype="real">
<input type="text">
</div>
...
And this CoffeeScript using jQuery: 而使用jQuery的CoffeeScript:
$('*[data-is_array="t"][data-simpletype="int"] :input').each ->
simpleCopy($(this))
$('*[data-is_array="t"][data-simpletype="char"] :input').each ->
simpleCopy($(this))
$('*[data-is_array="t"][data-simpletype="text"] textarea').each ->
simpleCopy($(this))
$('*[data-is_array="t"][data-simpletype="real"] :input').each ->
simpleCopy($(this))
In the HTML there are other inputs wrapped in div's with other data. 在HTML中,其他输入与其他数据一起包裹在div中。 What I want is to call
simpleCopy
on all inputs that have data-is_array="t"
and data-simpletype
is int
, char
, text
or real
. 我想要在所有具有
data-is_array="t"
且data-simpletype
为int
, char
, text
或real
输入上调用simpleCopy
。
My current solution works but is not realy nice. 我当前的解决方案有效,但并不是很好。 Is there a way to get all these elements with sth.
有没有办法让所有这些元素与某物。 like a logical OR selector to do this in one like:
就像一个逻辑OR选择器一样,可以做到这一点:
$('*[data-is_array="t"][data-simpletype="int||char||text||real"] :input||textarea').each ->
simpleCopy($(this))
I know there is a regex filter , but I don't want to use this because this is realy slow when I test this under Chrome. 我知道有一个regex过滤器 ,但我不想使用它,因为当我在Chrome上进行测试时,这确实很慢。 Is there a better jQuery method to do this?
有没有更好的jQuery方法来做到这一点?
You're complicating it too much. 您太复杂了。
<div id="foo">
<div data-is_array="t" data-simpletype="int">
<input type="text">
</div>
<div data-is_array="t" data-simpletype="char">
<input type="text">
</div>
<div data-is_array="t" data-simpletype="text">
<textarea></textarea>
</div>
<div data-is_array="t" data-simpletype="real">
<input type="text">
</div>
</div>
Wrap it all with a div 用div包装所有内容
$(' #foo input, #foo textarea ').each(function() {
your stuff ()...
});
You're good. 你很厉害。 Good programmers are lazy programmers.
好的程序员是懒惰的程序员。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.