admin管理员组文章数量:1434909
I'm involved in converting the jquery codes to java script for a requirement.
Example 1 :
There are a few child elements with class name "child_elems" inside a parent html div with id "parent_elem". I need to change the width of the child elements dynamically. The jquery code will look like this:
$(#parent_elem).find(.child_elems).each(function(){
$(this).css("width","50px");
});
Sample javascript:
var parentelem = document.getElementById("parent_elem");
var childelems = parentelem.getElementsByClassName("child_elems");
for(var i=0;i<childelems.length;i++)
childelems[i].style.width = "50px";
This works but I m not sure whether this is an efficient way.
Can we do the same task without using for loop? Is there any equivalent for .each
, .next
, .find
in javascript?
I'm very new to javascript and jquery as well, and would be thankful for any suggestions.
I'm involved in converting the jquery codes to java script for a requirement.
Example 1 :
There are a few child elements with class name "child_elems" inside a parent html div with id "parent_elem". I need to change the width of the child elements dynamically. The jquery code will look like this:
$(#parent_elem).find(.child_elems).each(function(){
$(this).css("width","50px");
});
Sample javascript:
var parentelem = document.getElementById("parent_elem");
var childelems = parentelem.getElementsByClassName("child_elems");
for(var i=0;i<childelems.length;i++)
childelems[i].style.width = "50px";
This works but I m not sure whether this is an efficient way.
Can we do the same task without using for loop? Is there any equivalent for .each
, .next
, .find
in javascript?
I'm very new to javascript and jquery as well, and would be thankful for any suggestions.
Share edited Jul 1, 2013 at 11:53 nnnnnn 150k30 gold badges209 silver badges247 bronze badges asked Jul 1, 2013 at 11:51 DheviDhevi 231 silver badge4 bronze badges 3-
Why do you want to avoid a
for
loop? That's the natural choice to perform some action on a list of items. jQuery's.each()
method is implemented using afor
loop. jQuery is "just" a collection of functions written in JavaScript, so you can see how the functions you mentioned work if you look in the jQuery.js file. – nnnnnn Commented Jul 1, 2013 at 11:56 - Why would you want to eliminate jQuery? – amhed Commented Jul 1, 2013 at 11:57
- "Is there any equivalent for .each , .next, .find in javascript"? If there was a cross-browser equivalent why would jQuery team implement thir versions? – sabithpocker Commented Jul 1, 2013 at 12:10
3 Answers
Reset to default 3You may use below for your work, but beware they might not work with all browsers. It is always safe to use jQuery if possible.
// jQuery -> JavaScript
.each -> array.forEach(callback[, thisArg])
.find -> document.querySelector(selectors), document.querySelectorAll(selectors);
You can definitely do this if you must, jQuery is just a layer that sits on top of javascript. When you write jQuery, you're writing javascript.
If you really need to go down this route, it might be helpful to look at the jQuery source. James Padolsey made a really helpful tool for inspecting the various methods that jQuery provides. You can see the tool here: http://james.padolsey./jquery/#v=git&fn=jQuery.fn.next.
Keep in mind that the jQuery team has spent years getting their code to work efficiently and for multiple browsers. Please don't be surprised if your code fails across browsers.
You could start looking at these functions code at github. .find = https://github./jquery/jquery/blob/master/src/traversing.js#L13 .each = https://github./jquery/jquery/blob/master/src/core.js#L541
本文标签: converting jquery(eachfindnext) to javascriptStack Overflow
版权声明:本文标题:converting jquery(.each, .find,.next) to javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744650142a2617634.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论