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

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

Hide parent element not containing specific text

问题

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

到目前为止,我有这个:

HTML

<div>
    <label>
        名字 <font color="red">*</font>
        <input type="text" id="guest_first_name">
    </label>
</div>

</br>

<div>
    <label>
        姓 
        <input type="text" id="guest_last_name">
    </label>
</div>

jQuery

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>

<script>
 $(document).ready(function() {

    let checkthis = $("#guest_first_name, #guest_last_name").parent();  
    var removewithout = "*";
    checkthis.not(removewithout).hide();

 })

</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

<div>
    <label>
        First name <font color="red">*</font>
        <input type="text" id="guest_first_name">
    </label>
</div>

</br>

<div>
    <label>
        Last name 
        <input type="text" id="guest_last_name">
    </label>
</div>

jQuery

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>

<script>
 $(document).ready(function() {

    let checkthis = $("#guest_first_name, #guest_last_name").parent();  
    var removewithout = "*";
    checkthis.not(removewithout).hide();

 })

</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:

<div>
   <label>
      名字 <font color="red">*</font>
      <input type="text" id="guest_first_name">
  </label>
</div>

<br>

<div>
  <label>
      姓氏 
      <input type="text" id="guest_last_name">
  </label>
</div>

JQUERY:

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

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:

  &lt;div&gt;
     &lt;label&gt;
        First name &lt;font color=&quot;red&quot;&gt;*&lt;/font&gt;
        &lt;input type=&quot;text&quot; id=&quot;guest_first_name&quot;&gt;
    &lt;/label&gt;
  &lt;/div&gt;

 &lt;/br&gt;

 &lt;div&gt;
    &lt;label&gt;
        Last name 
        &lt;input type=&quot;text&quot; id=&quot;guest_last_name&quot;&gt;
    &lt;/label&gt;
 &lt;/div&gt;

JQUERY:

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

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

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

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

&lt;div&gt;
    &lt;label&gt;
        First name &lt;font color=&quot;red&quot;&gt;*&lt;/font&gt;
        &lt;input type=&quot;text&quot; id=&quot;guest_first_name&quot;&gt;
    &lt;/label&gt;
&lt;/div&gt;

&lt;br&gt;

&lt;div&gt;
    &lt;label&gt;
        Last name 
        &lt;input type=&quot;text&quot; id=&quot;guest_last_name&quot;&gt;
    &lt;/label&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 2

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

$(document).ready(function() {

  let checkthis = $("#guest_first_name, #guest_last_name").parent();
  var removewithout = /\*/;
  checkthis.not((i, e) => removewithout.test(e.innerHTML)).hide();

})

参考资料:

英文:

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

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

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

$(document).ready(function() {

  let checkthis = $(&quot;#guest_first_name, #guest_last_name&quot;).parent();
  var removewithout = /\*/;
  checkthis.not((i, e) =&gt; removewithout.test(e.innerHTML)).hide();

})

<!-- 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:

确定