(使用jQuery或其他方法)进行DOM操作异步吗? [英] is DOM manipulation (with jQuery or else) async?

查看:210
本文介绍了(使用jQuery或其他方法)进行DOM操作异步吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用selectWithCircle函数使用jQuery选择元素,并向其中添加一个活动"类.

I am using selectWithCircle function to select elements with jQuery and add a class 'active' to them.

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

然后通过对相同元素进行另一次选择,如果元素具有活动"类,我需要对它们进行某些操作

then by doing another selection on the same elements I need to do something with them if the element has class '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选择操作是否以Sync方式执行?

Am I sure that when I loop through the jQuery('label') selection the function selectWithCircle did its work?
More generally, is jQuery selection manipulation performed in Sync way?

推荐答案

设置输入值是同步的.您将可靠地获得跨浏览器的更新值.其他DOM操作(例如插入元素,移除元素,移动元素)也是同步的,尽管在JavaScript代码完成之前用户可能看不到结果,让浏览器使用线程重绘显示内容.

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.

您的代码运行正常

这篇关于(使用jQuery或其他方法)进行DOM操作异步吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆