转换扫描的className的JavaScript块使用jQuery [英] Convert Scanning For className JavaScript block to use jQuery

查看:188
本文介绍了转换扫描的className的JavaScript块使用jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小方块,我想转换为使用jQuery的一对夫妇不同的目的,但主要是通过逆向工程它是如何工作来imporve我的jQuery的技能。我试图采取走它,但无法弄清楚所有的转换。

I have a small block I wanted to convert to using jQuery for a couple of different purposes, but mainly to reverse engineer how it works to imporve my jQuery skills. I tried taking a go at it, but could not figure out all of the conversions.

通过在ASP.NET TreeView控件的客户端提供的复选框迭代和扫描复选框以的className = disabledTreeviewNode以下的JavaScript (这equivilent功能的无法的实现纯粹的服务器端)。

The following Javascript block iterated through the checkboxes rendered in an ASP.NET TreeView control client-side and scan for checkboxes with a className=disabledTreeviewNode (this equivilent functionality cannot be achieved purely server side).

  function DisableCheckBoxes(treeviewClientID) {

     var treeView = document.getElementById(treeviewClientID);       
     if (treeView) {

        //Get all the checkboxes which are 'inputs' in the treeview
        var childCheckBoxes = treeView.getElementsByTagName("input");

        //Iterate through the checkboxes and disable any checkbox that has a className="disabledTreeviewNode"
        for (var i = 0; i < childCheckBoxes.length; i++) {

           var textSpan = childCheckBoxes[i].parentNode.getElementsByTagName("span")[0];
           if (textSpan != null && textSpan.firstChild)
              if (textSpan.className == "disabledTreeviewNode" || textSpan.firstChild.className == "disabledTreeviewNode")
                 childCheckBoxes[i].disabled = true;
        }
     }
  }

我试图改变如下:

I tried changing the following:

var treeView = document.getElementById(treeviewClientID); 

var treeView = $('#' + treeviewClientID); 

不过后来我再也不能叫的getElementsByTagName 。我试图使用jQuery的equivilent .find 但随后的code开始表现不同,我是有点失落。

However then I could no longer call getElementsByTagName. I tried to use the jQuery equivilent of .find but then the code started to behave differently and I was a bit lost.

任何人都可以帮助有关转换这个小方块使用jQuery?评论,欢迎来,如果这是值得的,甚至如果有更好的办法。

Can anyone assist on converting this small block to use jQuery? Comments are welcome as to if this is worthwhile or even if there is a better way.

编辑::该类= disabledTreeviewNode 是分配服务器端是这样的:

This class=disabledTreeviewNode is assigned server-side like this:

tn.Text = "<span class=disabledTreeviewNode>" + tn.Text + "</span>";

这是一个黑客位/标志,使客户端code可以读取它,并设置它的的的是复选框以禁用。为什么家长?我不能直接设置在code中的复选框类,因为该属性是不可访问。黑客:设置 TreeView控件对象的。文本&LT;跨度类= disabledTreeviewNode&GT ; 值,然后将它的父(复选框)残疾客户端

It's a bit of a hack/flag so that client-side code can read it and set it's parent which is the checkbox to disabled. Why the Parent? I can't directly set the class on the checkbox in code because that property is not accessible. The hack: set the TreeView object's .Text to have the <span class=disabledTreeviewNode> value and then set it's parent (the checkbox) to disabled client-side.

推荐答案

很多的jQuery对象的方法调用。每()的方法,所以你不吨有通过收集迭代,jQuery不会这个给你。

Many of the jQuery object's methods call .each() method behind the scene, so you don't have to iterate through the collection, jQuery does this for you.

$('#' + treeviewClientID + ' input').filter(function() {
     return $(this.parentNode).find('.disabledTreeviewNode').length;
}).prop('disabled', true);

这篇关于转换扫描的className的JavaScript块使用jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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