简体   繁体   English

将 $(this) 选择器与 [attribute*=value] 混合

[英]Mixing $(this) selector with [attribute*=value]

I've been digging documents and the forum to find a way to mix both selectors, but I didn't.我一直在挖掘文档和论坛以找到一种混合两种选择器的方法,但我没有。 I only know how to select all elements that [attribute*=value] (ex: $("[attribute*='value']") ), or all HTML elements from the same type (Ex: $("input[name*='value']") . How do I select something like $("$(this)[name*='value']") .我只知道如何 select [attribute*=value]的所有元素(例如: $("[attribute*='value']") ),或来自同一类型的所有 HTML 元素(例如: $("input[name*='value']") 。我如何 select 类似$("$(this)[name*='value']")

EDIT:编辑:

In this case, I'm using the selector.class.在这种情况下,我使用的是选择器.class。 But just for the propose of example.但只是为了举例。 I need to select both "more-bed" buttons.我需要 select 两个“更多床”按钮。 But all buttons of class "more" are firing the alert.但是 class “更多”的所有按钮都在发出警报。

 $(".more").click(function() { if ($(".more").filter('[name*=bed]')) { alert("bed") } });
 button { width: 100px; height: 30px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button class="more" name="bed-test">more-bed</button> <button class="more" name="bed-check">more-bed</button> <button class="less" name="bed-test">less-bed</button>

You can use not()你可以使用not()

 $('[name=bed-test]').not('.less').on('click', () => console.log('yay'))
 button { width: 100px; height: 30px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button class="more" name="bed-test">more-bed</button> <button class="less" name="bed-test">less-bed</button> <button class="more" name="bath-test">more-bath</button>

You can also use filter()您也可以使用filter()

 $('.more').on('click', e => $(e.currentTarget).filter('[name=bed-test]').css('width', '300px'))
 button { width: 100px; height: 30px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button class="more" name="bed-test">more-bed</button> <button class="less" name="bed-test">less-bed</button> <button class="more" name="bath-test">more-bath</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM