简体   繁体   English

使用javascript嵌套在div中的已修改html元素

[英]modified html element nested in div using javascript

I'm trying to change a style on a UL that is nested in a set of divs. 我正在尝试更改嵌套在一组div中的UL的样式。

Here is what I have: 这是我所拥有的:

Html: HTML:

<div class='mainCat' id='List' onclick="Expand(this.id);">List</div>
  <div class='subCat'>
  <ul>
  <li>List item one</li>
   <li>List item two</li>
  <li>List item three</li>
</ul>
 </div>
</div>

and Javascript: 和Javascript:

function Expand(set)
 {
  var whichSet = document.getElementById(set);
  whichSet.getElementsByTagName('ul')[0].style.display = 'block';
 }

and CSS: 和CSS:

 .subCat ul    
  {
  display:none;
  }

Any help is much appreciated. 任何帮助深表感谢。 An explanation of what i'm doing wrong instead of just the answer would be really helpful as i like to learn from my mistakes. 我想从错误中学习,对我正在做错的事情(而不只是答案)做出的解释将非常有帮助。 Thanks in advance. 提前致谢。

I have created a working JSFiddle with your case. 我已经根据您的情况创建了一个可工作的JSFiddle

Your DOM structure is incorrect. 您的DOM结构不正确。 You have a closing div tag just before List element. 您在List元素之前有一个div结束标签。 You should remove it. 您应该删除它。

Furthermore, I would have not use an inline event, as it is a bad practice. 此外,我不会使用内联事件,因为这是一种不好的做法。 I moved it to a addEventListener function, which splits better DOM structure of Javascript events 我将其移至addEventListener函数,该函数可拆分Javascript事件的更好DOM结构

So, your code becomes: 因此,您的代码变为:

<div class='mainCat' id='List'>
    List
    <div class='subCat'>
        <ul>
            <li>List item one</li>
            <li>List item two</li>
            <li>List item three</li>
        </ul>
     </div>
</div>

And adds the following to your Javascript: 并将以下内容添加到您的Javascript中:

document.getElementById("List").addEventListener("click", function(e) {
    Expand(this.id);
}, true);

It should works then. 那应该可以了。

You are using getElementsByTagName inside the "set" element, but that is the mainCat div, not the subCat div. 您正在“ set”元素内使用getElementsByTagName ,但这是mainCat div,而不是subCat div。 The ul is not child of that div. ul不是该div的子级。

Also, you don't need the this.id part. 另外,您不需要this.id部分。

I changed the code, so subCat is an id instead. 我更改了代码,所以subCat代替了id。 (Maybe you intended that subCat were a child of mainCat ? if that is the case, you have an extra </div> next to the word List ) (也许您打算让subCatmainCat ?如果是这种情况,在List一词旁边会有一个</div>

Here is a fixed example: http://jsfiddle.net/4ndEf/ 这是一个固定的示例: http : //jsfiddle.net/4ndEf/

js: JS:

function Expand(set) {
    var whichSet = document.getElementById(set);
    whichSet.getElementsByTagName('ul')[0].style.display = 'block';
}

html: HTML:

<div class='mainCat' id='List' onclick="Expand('subCat');">List</div>
<div id='subCat'>
    <ul>
        <li>List item one</li>
        <li>List item two</li>
        <li>List item three</li>
    </ul>
</div>

css: CSS:

 #subCat ul {
     display:none;
 }

As chris and Jonathan Petitcolas said. 正如克里斯和乔纳森·皮蒂科拉斯所说。

But you can also simplify it with jQuery 但是您也可以使用jQuery简化它

$("#list").click(function(){
    $('ul').css('display', 'block');
});

I would set this CSS on list, so the user can see it is clickable 我将此CSS设置在列表上,以便用户可以看到它是可单击的

#list {
    cursor: pointer;
}

Fiddle here http://jsfiddle.net/uYuST/ 在这里拨弄http://jsfiddle.net/uYuST/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM