gpt4 book ai didi

jQuery $(this) 语法问题

转载 作者:行者123 更新时间:2023-12-01 06:13:13 25 4
gpt4 key购买 nike

这是一个有效的选择器吗?如果不是,正确的方法是什么?

$($(this)+' childElement')....

最佳答案

这可能就是您正在寻找的:

$('childElement', $(this))

基本上,它会在当前元素 this 的上下文中搜索 childElement

<小时/>

有关详细信息,请参阅 jQuery() 的文档功能。特别是,以下摘录解释了第二个参数以及它如何等效于使用 find:

By default, selectors perform their searches within the DOM starting at the document root. However, an alternate context can be given for the search by using the optional second parameter to the $() function. For example, if within a callback function we wish to do a search for an element, we can restrict that search:

$('div.foo').click(function() {
$('span', this).addClass('bar');
});

Since we've restricted the span selector to the context of this, only spans within the clicked element will get the additional class.

Internally, selector context is implemented with the .find() method, so $('span', this) is equivalent to $(this).find('span').

关于jQuery $(this) 语法问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4922285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com