简体   繁体   English

Javascript重叠-如何修复切换的显示/隐藏div

[英]Javascript Overlap - How do fix toggled show/hide div

I have a webpage with two buttons on it. 我有一个带有两个按钮的网页。 One button SHOULD open a div that says 'no dates'. 一个按钮应该打开一个显示“无日期”的div。 The other SHOULD open a div that says 'available.' 另一个应该打开一个表示“可用”的div。

Button 1 HTML/Script: 按钮1 HTML /脚本:

<script>
var toggle = function() {
var mydiv = document.getElementById('nodate');
if (mydiv.style.display === 'block' || mydiv.style.display === '')
mydiv.style.display = 'none';
else
mydiv.style.display = 'block'
}
</script>

<div id="nodate" style="display:none"><span class="A"> <input type="image" src="http://www.weebly.com/uploads/1/6/7/6/16768236/custom_themes/307429121386408805/files/close-icon.png?1384992227202" width="42" onclick="toggle();" /></span><b>There are no reservations listed under the date you selected. </b>Please select another date or ask for assistance.<br><br><image src=”http://www.weebly.com/uploads/1/6/7/6/16768236/custom_themes/307429121386408805/files/sorry.jpg?1384992565622” width=”200”/></div>
<input type="submit" style="background-color: #fff; padding: 4px; border: solid 2px #000; cursor:pointer;" value="Friday November 22nd, 2013" onclick="toggle();" class=”days1”></input>
<br><br>

Button 2 HTML/Script: 按钮2 HTML /脚本:

<script>
var toggle = function() {
var mydiv = document.getElementById('nodate2');
if (mydiv.style.display === 'block' || mydiv.style.display === '')
mydiv.style.display = 'none';
else
mydiv.style.display = 'block'
}
</script>

<div id="nodate2" style="display:none"><span class="A"> <input type="image" src="http://www.weebly.com/uploads/1/6/7/6/16768236/custom_themes/307429121386408805/files/close-icon.png?1384992227202" width="42" onclick="toggle();" /></span><b>Cool! We’ve got reservations for the date you selected. </b><br><u>Your name must use a proper format or you will be redirected to this page until a correct format and reserved name are used.</u><ul><li>&bull; Both First &amp; Last</li><li>&bull;
 First &amp; Last name must have first letter capitalized</li><li>&bull;
 Single space between first &amp; last name</li></ul>Ex: Austin Block

<br><br><image img style="border:2px solid #000" src=”http://www.weebly.com/uploads/1/6/7/6/16768236/custom_themes/307429121386408805/files/reservations2.jpg?1385069953644” width=”200”/></div>
<input type="submit" style="background-color: #fff; padding: 4px; border: solid 2px #000; cursor:pointer;" value="Saturday November 23rd, 2013" onclick="toggle();" class=”days1”></input>

The problem: When I launch the page, only one of the two hidden divs shows up. 问题:启动页面时,仅显示两个隐藏的div之一。 Both buttons open the SAME one. 两个按钮都打开相同的一个。 So basically, I think I need to separate the two scripts. 因此,基本上,我想我需要将两个脚本分开。 I cannot do Javscript at all (even though went to a class several times). 我根本无法做Javscript(即使上过几次课)。 I tried adding an 'a' to the Javascript function across one button, but it refused to work. 我尝试通过一个按钮在Javascript函数中添加一个“ a”,但是它拒绝工作。

Try something like this: 尝试这样的事情:

<script>
    function toggleButton(elementID) {
        var mydiv = document.getElementById(elementID);
        if (mydiv.style.display === 'block' || mydiv.style.display === '')
            mydiv.style.display = 'none';
        else
        mydiv.style.display = 'block'
    }
</script>

Then on the input: 然后在输入:

onclick="toggle('elementID');"

There are ways to automate it, but this should be effective. 有一些方法可以使其自动化,但这应该是有效的。

The problem before is that you were overriding the existing function. 以前的问题是您要覆盖现有功能。 You either need to create a separate function or add an argument to the function. 您需要创建一个单独的函数或向该函数添加一个参数。

You are overwriting toggle method, But there are other better ways you can do this. 您正在覆盖toggle方法,但是还有其他更好的方法可以执行此操作。 One way you can do it based on your html is like this, you do not have to set up an onclick for each of them. 您可以基于html的一种方法是这样,您不必为每个参数都设置onclick。

Html: HTML:

<input type="image" src="http://www.weebly.com/uploads/1/6/7/6/16768236/custom_themes/307429121386408805/files/close-icon.png?1384992227202" width="42" onclick="collapse.call(this);" />

and

 <input type="submit" style="background-color: #fff; padding: 4px; border: solid 2px #000; cursor:pointer;" value="Friday November 22nd, 2013" onclick="toggle.call(this);" class="days1"></input>

JS: JS:

var toggle = function () {
    var mydiv =this.previousElementSibling;
    mydiv.style.display = (mydiv.style.display || 'block') === "block" ? 'none' :'block'
}
var collapse = function(){
    this.parentElement.parentElement.style.display = 'none';
}

Demo 演示版

Another way which would be that set up a data-target attribute on your buttons to point to the target divs, and add a class name for them, and then attach an event to the buttons rather that attaching onclick attribute and perform the operation retrieving your targets. 另一种方法是在按钮上设置数据目标属性以指向目标div,并为其添加类名,然后将事件附加到按钮,而不是附加onclick属性并执行检索您的操作的操作目标。

You might find this easier to do in jQuery. 您可能会发现在jQuery中更容易做到这一点。 For one, it's automatically cross-browser compatible (no manual adjustments to cover off IE abnormalities), and it's much less typing. 一方面,它是跨浏览器自动兼容的(无需手动调整即可掩盖IE异常),而且键入更少。 Many people also find it generally easier to learn. 许多人还发现它通常更容易学习。 I did. 是的

To use jQuery, you just need to load the jQuery library in your document, usually in the <head> tags, like this: 要使用jQuery,您只需要在文档中加载jQuery库,通常在<head>标记中,如下所示:

<head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<head>

jsFiddle demo here jsFiddle演示在这里

Here is what your jQuery code would look like: 这是您的jQuery代码如下所示:

$('.days1').click(function() {
    var v = $(this).val();
    if (v.substr(0,3)=='Fri'){
        $('#nodate2').hide();
        $('#nodate').show();
    }else if (v.substr(0,3)=='Sat') {
        $('#nodate2').show();
        $('#nodate').hide();
    }
});

$('img').click(function() {
    // $(this) refers to whatever was clicked on, one of the img tags
    // .parent() is the div tag above this img tag
    $(this).parent().toggle();
});

Please examine the jsFiddle HTML code closely, as I made a number of changes. 由于我做了很多更改,请仔细检查jsFiddle HTML代码。 Note that your <img> tags were not correct, as well as the <input> tags. 请注意,您的<img>标记以及<input>标记均不正确。

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

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