gpt4 book ai didi

javascript - DOM 操作(使用 jQuery 或其他)是异步的吗?

转载 作者:行者123 更新时间:2023-11-28 14:48:30 26 4
gpt4 key购买 nike

我正在使用 selectWithCircle 函数通过 jQuery 选择元素并向其添加“active”类。

var selectWithCircle = function(el){
if(!el.hasClass('active')){
el.addClass('active');
}
}

然后,如果元素具有“active”类,则通过对相同元素进行另一次选择,我需要对它们执行某些操作

jQuery('label.ico-btn').click(function(event){
var $this = jQuery(this);

selectWithCircle($this);

jQuery('label').each(function(index,item){//am I sure that this runs after selectWithCircle()?
if(jQuery(item).hasClass('active')){
//do something
}
});
});

我确定当我循环遍历 jQuery('label') 选择时,函数 selectWithCircle 会起作用吗?
更一般地说,jQuery 选择操作是否以同步方式执行?

最佳答案

Setting the value of an input is synchronous. You will reliably get the updated value, cross-browser. Other DOM manipulations like inserting elements, removing them, moving them are also synchronous, although the user may not see the result until your JavaScript code completes, letting the browser use the thread to repaint the display.

你的代码工作正常

关于javascript - DOM 操作(使用 jQuery 或其他)是异步的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45481863/

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