简体   繁体   English

如何使用Javascript解决整个可点击div?

[英]How to solve whole clickable div with Javascript?

I have a problem with my Javascript . 我的Javascript有问题。 I have a collapsible area which has content and input fields for my users to write something on. 我有一个可折叠区域,其中包含内容和输入字段,供用户在上面写东西。 I have made a JavaScript function that collapses and opens the div, but the problem is that you can't write something on the input fields in this area, because the whole div is clickable. 我制作了一个JavaScript函数,它可以折叠并打开div,但是问题是您无法在此区域的输入字段中写东西,因为整个div都是可单击的。 I don't want the whole div to be clicakble, but only the area with the title to be clickable. 我不希望整个div变得混乱,而只希望标题可点击的区域。

How can I make only, and only the header, clickable? 我怎样才能使标题只有标题可点击?

HTML: Test title blablbala HTML:测试标题blablbala

<div class="area">Test title
       <div class="some content">blablbala
          <input></input>
       </div>
    </div>

JS: JS:

function areaCollapse() {
    var next = this.querySelector(".content");

    if (this.classList.contains("open")) {
        next.style.display = "none";
        this.classList.remove("open");
    } else {
        next.style.display = "block";
        this.classList.add("open");
    }
}

    var classname = document.getElementsByClassName("area");

    for(var i=0;i<classname.length;i++){
        classname[i].addEventListener('click', areaCollapse, true);
    }

http://jsfiddle.net/1BJK903/nb1ao39k/6/ http://jsfiddle.net/1BJK903/nb1ao39k/6/

I think that I have to change the structure of my HTML. 我认为我必须更改HTML的结构。 I tried this already: 我已经尝试过了:

Check this fiddle if this is what you wanted: http://jsfiddle.net/nb1ao39k/5/ 如果您想要的是,请检查此小提琴: http : //jsfiddle.net/nb1ao39k/5/

I added this: 我添加了这个:

if(this != evt.target){
    return;
}// if the element that listened to the event is not the element who was clicked then ignore

Check whether the event went to an input element before bubbling: 冒泡之前,请检查事件是否进入输入元素:

function areaCollapse(event) {
    if (event.target.tagName == "INPUT") {
        return;
    }
    ...
}

Alternatively, only allow clicks on the title, not the rest of the div. 或者,仅允许单击标题,而不允许div的其余部分。

Working Version : 工作版本:

 function areaCollapse() { var next = this.parentNode.children[1]; if (this.classList.contains("open")) { next.style.display = "none"; this.classList.remove("open"); } else { next.style.display = "block"; this.classList.add("open"); } } var classname = document.getElementsByClassName("title"); for(var i=0;i<classname.length;i++){ classname[i].addEventListener('click', areaCollapse, true); } 
 .content { display: none; } 
 <div class="area"><div class='title'>Test title</div> <div class="some content">blablbala <input></input></div> </div> <div class="area"><div class='title'>Test title</div> <div class="some content">blablbala <input></input></div> </div> 

You could try this: 您可以尝试以下方法:

HTML: HTML:

<div class="area">
    <h2 class="clickable-header">Test title</h2>
       <div class="some content" style="display: none">blablbala
          <input></input>
       </div>
    </div>

<div class="area">
    <h2 class="clickable-header">Test title</h2>
       <div class="some content">blablbala
          <input></input>
       </div>
    </div>

JS: JS:

function areaCollapse() {
    var next = this.nextElementSibling;

    if (this.classList.contains("open")) {
        next.style.display = "none";
        this.classList.remove("open");
    } else {
        next.style.display = "block";
        this.classList.add("open");
    }
}

    var classname = document.getElementsByClassName("clickable-header");

    for(var i=0;i<classname.length;i++){
        classname[i].addEventListener('click', areaCollapse, true);
    }

Just adds a heading element which is the clickable portion. 只需添加一个可点击部分的标题元素。

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

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