得到固定的孩子

时间:2014-04-28 02:58:50

标签: javascript treenode

我有这样的HTML:

<div class = "touch" onclick="do(this)">
  <span>text01</span>
  <span>text02</span>
  <span>text03</span>
  <span>text04</span>
  <div class = "findMe">andHideMe</div>
</div>
<div class = "touch" onclick="do(this)">
  <span>text01</span>
  <span>text02</span>
  <div class = "findMe">andHideMe</div>
</div>
<div class = "touch" onclick="do(this)">
  <span></span>
  <span>just</span>
  <div class = "findMe">andHideMe</div>
</div>

当我点击TOUCH时,DIV'FindMe'可以做什么。

如何使用此选项在TOUCH中选择'FindMe' 使用JavaScript ???

function do(obj)
{
    var se = obj.childNodes;
    for (var t = 0; t < obj.childNodes.length; t++)
    {
        if (se[t].className == 'findMe')
        {
            se[t].className.style.display = 'none';
        };

    }
}

2 个答案:

答案 0 :(得分:0)

变化:

se[t].className.style.display = 'none';

se[t].style.display = 'none';

您可能希望包含一个简单的 hasClass 函数,如:

function hasClass(element, className) {
    var re = new RegExp('(^|\\s)' + className + '(\\s|$)');
    return re.test(element.className);
}

// or

function hasClass(element, className) {
    return (' ' + element.className + ' ').indexOf(' ' + className + ' ') > -1;
}

然后使用它:

if (hasClass(se[t], className)) {
  // do stuff
}

答案 1 :(得分:0)

jQuery找到div类。

var findMeDivClass = $(&#39; .findMe&#39;);

和id find:var findMeDivId = $(&#39;#findMe&#39;);

和attr find:var findMeDivAttr = $(&#39; [name =&#34; findMe&#34;]&#39;);