英文:
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:
<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:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$(document).ready(function() {
let checkthis = $("input['text']").parent();
var removewithout = /\*/;
checkthis.not((i, e) => removewithout.test(e.innerHTML)).hide();
});
<!-- language: lang-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>
<!-- 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 = $("#guest_first_name, #guest_last_name").parent();
var removewithout = /\*/;
checkthis.not((i, e) => removewithout.test(e.innerHTML)).hide();
})
<!-- end snippet -->
Reference:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论