将jquery(.each,.find,.next)转换为javascript [英] converting jquery(.each, .find,.next) to javascript

查看:85
本文介绍了将jquery(.each,.find,.next)转换为javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将jquery代码转换为Java脚本.

I'm involved in converting the jquery codes to java script for a requirement.

示例1:

在ID为"parent_elem"的父html div中有一些类名为"child_elems"的子元素.我需要动态更改子元素的宽度. jQuery代码将如下所示:

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");
 });

示例javascript:

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.

我们可以在不使用for循环的情况下完成相同的任务吗? javascript中的.each.next.find是否有等效项?

Can we do the same task without using for loop? Is there any equivalent for .each , .next, .find in javascript?

我也是javascript和jquery的新手,感谢您的任何建议.

I'm very new to javascript and jquery as well, and would be thankful for any suggestions.

推荐答案

如果必须的话,您绝对可以这样做,jQuery只是位于javascript之上的一层.当您编写jQuery时,您正在编写javascript.

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.

如果您真的需要走这条路,那么查看jQuery源可能会有所帮助. James Padolsey为检查jQuery提供的各种方法提供了一个非常有用的工具.您可以在此处查看该工具: http://james.padolsey .com/jquery/#v = git& fn = jQuery.fn.next .

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.com/jquery/#v=git&fn=jQuery.fn.next.

请记住,jQuery团队已经花了来使他们的代码有效地运行并适用于多个浏览器.如果您的代码在浏览器中失败,请不要感到惊讶.

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.

这篇关于将jquery(.each,.find,.next)转换为javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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