jQuery查找前提?

前端之家收集整理的这篇文章主要介绍了jQuery查找前提?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当我想要找到一个对象的孩子时,我可以使用children();当我想在另一个对象中找到一个对象时,不一定是小孩,可以使用find().如果我想找一个父母,我使用parent(),但是如果我想找到一个先行者,不知道是否是父母祖父母,祖父母,我该怎么办?

我会给你一个例子:我正在构建一个应用于一些“输入:文本”的插件.

最后,我需要做一些事情来保持他们的形式.但有时,文本框将直接在表单中,或者它们可以在无序列表或表内.

我能否以一般的方式参考表格?

解决方法

您可以使用jQuery的最近()方法
  1. $('input:text').change(
  2. function(){
  3. var ancestorFormElement = $(this).closest('form');
  4. // do stuff.
  5. });
  1. $('input:text').change(function() {
  2. var ancestorFormElement = $(this).closest('form');
  3.  
  4. ancestorFormElement.addClass('hasInputChanged');
  5. });
  1. form {
  2. border: 2px solid #000;
  3. padding: 1em;
  4. }
  5. form.hasInputChanged {
  6. border-color: limegreen;
  7. }
  1. <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  2. <form action="#" method="post">
  3. <fieldset>
  4. <legend>Simple demo</legend>
  5. <label for="name">Text Input:</label>
  6. <input type="text" name="name" id="name" value="" tabindex="1" />
  7. </fieldset>
  8. </form>

外部JS Fiddle demo,用于实验或开发.

或者,您可以简单地使用将< form>元素与其后代的形式元素(< input>,< textarea>,< select>等):

  1. $('input:text').change(function() {
  2. var ancestorFormElement = this.form;
  3.  
  4. // because 'this.form' returns a DOM node,it
  5. // must be converted to a jQuery object in
  6. // order to utilise jQuery methods:
  7. $(ancestorFormElement).addClass('hasInputChanged');
  8. });
  1. $('input:text').change(function() {
  2. var ancestorFormElement = this.form;
  3.  
  4. $(ancestorFormElement).addClass('hasInputChanged');
  5. });
  1. form {
  2. border: 2px solid #000;
  3. padding: 1em;
  4. }
  5. form.hasInputChanged {
  6. border-color: limegreen;
  7. }
  1. <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  2. <form action="#" method="post">
  3. <fieldset>
  4. <legend>Simple demo</legend>
  5. <label for="name">Text Input:</label>
  6. <input type="text" name="name" id="name" value="" tabindex="1" />
  7. </fieldset>
  8. </form>

外部JS Fiddle demo,用于实验或开发.

此外,因为我强烈怀疑你想要改变 – 无论如何 – 要恢复“改变”,我建议采取以下方法

  1. $('input:text').change(function() {
  2. var ancestorFormElement = this.form;
  3.  
  4. // here we use the 'toggleClass(<class-name>,<switch>)'
  5. // method; where the 'switch' returns a Boolean true/false
  6. // if it evaluates to true then the class-name is added
  7. // and if it evaluates to false then the class-name is
  8. // removed:
  9. $(ancestorFormElement).toggleClass('hasInputChanged',this.value !== this.defaultValue);
  10. });
  1. $('input:text').change(function() {
  2. var ancestorFormElement = this.form;
  3.  
  4. $(ancestorFormElement).toggleClass('hasInputChanged',this.value !== this.defaultValue);
  5. });
  1. form {
  2. border: 2px solid #000;
  3. padding: 1em;
  4. }
  5. form.hasInputChanged {
  6. border-color: limegreen;
  7. }
  1. <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  2. <form action="#" method="post">
  3. <fieldset>
  4. <legend>Simple demo</legend>
  5. <label for="name">Text Input:</label>
  6. <input type="text" name="name" id="name" value="" tabindex="1" />
  7. </fieldset>
  8. </form>

外部JS Fiddle demo,完全可以将更改事件处理程序委派给< form>元素本身,使用on():

  1. $('form').on('change',function(e) {
  2.  
  3. // here 'e' is the event-object passed to the
  4. // event-handling function; 'e.target' is the
  5. // element that received the initiating event:
  6. var changedEl = e.target;
  7.  
  8. $(this).toggleClass('hasInputChanged',changedEl.value !== changedEl.defaultValue);
  9. });
  1. $('form').on('change',function(e) {
  2. var changedEl = e.target;
  3.  
  4. $(this).toggleClass('hasInputChanged',changedEl.value !== changedEl.defaultValue);
  5. });
  1. form {
  2. border: 2px solid #000;
  3. padding: 1em;
  4. }
  5. form.hasInputChanged {
  6. border-color: limegreen;
  7. }
  1. <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  2. <form action="#" method="post">
  3. <fieldset>
  4. <legend>Simple demo</legend>
  5. <label for="name">Text Input:</label>
  6. <input type="text" name="name" id="name" value="" tabindex="1" />
  7. </fieldset>
  8. </form>

外部JS Fiddle demo,用于实验或开发.

也可以将选择器传递给on()方法来指定应该启动给定事件的元素(这里是“change”事件)到
触发与祖先绑定的事件处理:

  1. // here we pass the selector,'input[type=text]' to the
  2. // method,which restricts the event-handling to only
  3. // those events originating with <input> elements whose
  4. // 'type' attribute is equal to 'text':
  5. $('form').on('change','input[type=text]',function(e) {
  6.  
  7. $(this).toggleClass('hasInputChanged',function(e) {
  8. var ancestorFormElement = this.form;
  9.  
  10. $(ancestorFormElement).toggleClass('hasInputChanged',this.value !== this.defaultValue);
  11. });
  1. form {
  2. border: 2px solid #000;
  3. padding: 1em;
  4. }
  5. form.hasInputChanged {
  6. border-color: limegreen;
  7. }
  1. <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  2. <form action="#" method="post">
  3. <fieldset>
  4. <legend>Simple demo</legend>
  5. <label for="name">Text Input:</label>
  6. <input type="text" name="name" id="name" value="" tabindex="1" />
  7. </fieldset>
  8. </form>

外部JS Fiddle demo,用于实验或开发.

最后,一个纯粹的JavaScript手段完成了同样的行为:

  1. // defining a named function to handle the
  2. // event-handling,the 'event' argument is
  3. // passed automagically from the
  4. // addEventListener() method (below):
  5. function changeHandler(event) {
  6.  
  7. // 'this' is the element to which
  8. // event-handler was bound (again
  9. // automagically passed from
  10. // addEventListener()):
  11. var form = this,changed = event.target;
  12.  
  13. // here we use the Element.classList API; which
  14. // works much as toggleClass() does,adding the
  15. // supplied class-name if the switch that follows
  16. // evaluates to true,removes it if the switch
  17. // evaluates to false:
  18. form.classList.toggle('hasInputChanged',changed.value !== changed.defaultValue);
  19.  
  20. }
  21.  
  22. // retrieving the <form> element using
  23. // document.querySelector(),which returns
  24. // the first element in the document that
  25. // matches the CSS selector passed to the
  26. // function:
  27. var formElement = document.querySelector('form');
  28.  
  29. // using addEventListener to bind the named
  30. // function (changeHandler) as the event-
  31. // handler for the 'change' event:
  32. formElement.addEventListener('change',changeHandler);
  1. function changeHandler(event) {
  2. var form = this,changed = event.target;
  3.  
  4. form.classList.toggle('hasInputChanged',changed.value !== changed.defaultValue);
  5.  
  6. }
  7.  
  8. var formElement = document.querySelector('form');
  9.  
  10. formElement.addEventListener('change',changeHandler);
  1. form {
  2. border: 2px solid #000;
  3. padding: 1em;
  4. }
  5. form.hasInputChanged {
  6. border-color: limegreen;
  7. }
  1. <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  2. <form action="#" method="post">
  3. <fieldset>
  4. <legend>Simple demo</legend>
  5. <label for="name">Text Input:</label>
  6. <input type="text" name="name" id="name" value="" tabindex="1" />
  7. </fieldset>
  8. </form>

外部JS Fiddle demo,用于实验或开发.

参考文献:

> CSS:

> Attribute selectors.

> JavaScript:

> document.querySelector().
> Element.classList.
> EventTarget.addEventListener().
> HTMLInputElement.

> jQuery:

> addClass().
> change().
> closest().
> on().
> toggleClass().

猜你在找的jQuery相关文章