隐藏不包含特定文本的父元素

huangapple go评论110阅读模式
英文:

Hide parent element not containing specific text

问题

我尝试使用子元素的ID作为选择器隐藏父元素;如果它们不包含特定文本,则使用 *。必须这样,并且必须使用jQuery。

到目前为止,我有这个:

HTML

  1. <div>
  2. <label>
  3. 名字 <font color="red">*</font>
  4. <input type="text" id="guest_first_name">
  5. </label>
  6. </div>
  7. </br>
  8. <div>
  9. <label>
  10. <input type="text" id="guest_last_name">
  11. </label>
  12. </div>

jQuery

  1. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
  2. <script>
  3. $(document).ready(function() {
  4. let checkthis = $("#guest_first_name, #guest_last_name").parent();
  5. var removewithout = "*";
  6. checkthis.not(removewithout).hide();
  7. })
  8. </script>

努力检查不包含文本的元素,然后隐藏。试图隐藏不包含 * 文本的 <label>,例如 。希望这清楚。感谢您的帮助。谢谢。

英文:

I am trying to hide parent elements using ID of child as selector; if they do not contain specific text. In this case *. Must be this way and have to use jQuery.

So far I have this:

HTML

  1. <div>
  2. <label>
  3. First name <font color="red">*</font>
  4. <input type="text" id="guest_first_name">
  5. </label>
  6. </div>
  7. </br>
  8. <div>
  9. <label>
  10. Last name
  11. <input type="text" id="guest_last_name">
  12. </label>
  13. </div>

jQuery

  1. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
  2. <script>
  3. $(document).ready(function() {
  4. let checkthis = $("#guest_first_name, #guest_last_name").parent();
  5. var removewithout = "*";
  6. checkthis.not(removewithout).hide();
  7. })
  8. </script>

Struggling to check elements that do not contain text and then hide. Trying to hide <label> with text Last name as it does not contain *. Hope this is clear. Any help is appreciated.

Thanks

答案1

得分: 4

.not()方法在jquery和javascript的.not选择器中只接受对象、选择器和函数作为参数。但你正在传递一个字符作为参数:

以下是更简洁的代码可以帮到你:

HTML:

  1. <div>
  2. <label>
  3. 名字 <font color="red">*</font>
  4. <input type="text" id="guest_first_name">
  5. </label>
  6. </div>
  7. <br>
  8. <div>
  9. <label>
  10. 姓氏
  11. <input type="text" id="guest_last_name">
  12. </label>
  13. </div>

JQUERY:

  1. $(document).ready(function() {
  2. let checkthis = $("input['text']").parent();
  3. var removewithout = /\*/;
  4. checkthis.not((i, e) => removewithout.test(e.innerHTML)).hide();
  5. });
英文:

The .not() method in jquery & .not selector in javascript only accepts the objects, selectors and functions as parameters.
But you are passing a character for that:

Here is the more brief code that can help you:

HTML:

  1. &lt;div&gt;
  2. &lt;label&gt;
  3. First name &lt;font color=&quot;red&quot;&gt;*&lt;/font&gt;
  4. &lt;input type=&quot;text&quot; id=&quot;guest_first_name&quot;&gt;
  5. &lt;/label&gt;
  6. &lt;/div&gt;
  7. &lt;/br&gt;
  8. &lt;div&gt;
  9. &lt;label&gt;
  10. Last name
  11. &lt;input type=&quot;text&quot; id=&quot;guest_last_name&quot;&gt;
  12. &lt;/label&gt;
  13. &lt;/div&gt;

JQUERY:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. $(document).ready(function() {
  2. let checkthis = $(&quot;input[&#39;text&#39;]&quot;).parent();
  3. var removewithout = /\*/;
  4. checkthis.not((i, e) =&gt; removewithout.test(e.innerHTML)).hide();
  5. });

<!-- language: lang-html -->

  1. &lt;div&gt;
  2. &lt;label&gt;
  3. First name &lt;font color=&quot;red&quot;&gt;*&lt;/font&gt;
  4. &lt;input type=&quot;text&quot; id=&quot;guest_first_name&quot;&gt;
  5. &lt;/label&gt;
  6. &lt;/div&gt;
  7. &lt;br&gt;
  8. &lt;div&gt;
  9. &lt;label&gt;
  10. Last name
  11. &lt;input type=&quot;text&quot; id=&quot;guest_last_name&quot;&gt;
  12. &lt;/label&gt;
  13. &lt;/div&gt;

<!-- end snippet -->

答案2

得分: 2

.not() 方法接受选择器、jQuery 对象和函数。

  1. $(document).ready(function() {
  2. let checkthis = $("#guest_first_name, #guest_last_name").parent();
  3. var removewithout = /\*/;
  4. checkthis.not((i, e) => removewithout.test(e.innerHTML)).hide();
  5. })

参考资料:

英文:

The .not() method accepts selectors, jQuery objects, and functions.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. $(document).ready(function() {
  2. let checkthis = $(&quot;#guest_first_name, #guest_last_name&quot;).parent();
  3. var removewithout = /\*/;
  4. checkthis.not((i, e) =&gt; removewithout.test(e.innerHTML)).hide();
  5. })

<!-- end snippet -->

Reference:

huangapple
  • 本文由 发表于 2023年6月2日 10:53:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76386852.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定