开发者

How to keep jquery from descending too far?

开发者 https://www.devze.com 2023-01-03 09:19 出处:网络
I am having a problem with jquery going a bit too far on pattern matching of CSS classes and IDs. I have some markup that looks like this:

I am having a problem with jquery going a bit too far on pattern matching of CSS classes and IDs.

I have some markup that looks like this:

  <div id="blah">
    <div class="level2">
      <input type="text" />
    </div>
    <div class="levelA">
      <div class="level2">
        <input type="text" value="foo"/>
      </div>
    </div>
  </div>
  <input type="text" value="bar" />

I want for the 3 inputs to say

  • Hello
  • Foo
  • Bar

so I have this line of jquery:

$('#blah .level2 input').val('hello');

the problem now is that jquery is a bit too liberal in it's pattern matche开发者_Python百科s and matches both the first and second.

How can I prevent this kind of thing from happening?

A live example is at http://jsbin.com/opelo3/4


You want

$('#blah > .level2 input').val('hello');

> means direct descendant.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号