简体   繁体   English

确认后使div在JavaScript中不可见

[英]Make a div invisible in JavaScript after confirmation

I want to make a div visible when clicking on a button. 我想在单击按钮时使div可见。 Button should ask Yes/No confirmation. 按钮应询问是/否确认。 Div should be visible only when user clicks on 'Yes'. 仅当用户单击“是”时, Div应该可见。

My code is here 我的代码在这里

    <div id="Mydiv" style="display:none;" >Haiii</div>
    <input type="button" name="answer" value="Show Div" onclick="confirm_hide(this)"/>

JavaScript 的JavaScript

function confirm_hide(ele){

   if (confirm('Do you wish to hide?')) {
   ele.style.display = 'none';
   document.getElementById('Mydiv').style.display = 'block';
    return true;
   } else return false;
    }

 function clicked() { var name = document.getElementById('name').value; if(confirm('Hello ' + name + ', great to see you!')) { document.getElementById('nameDiv').innerHTML = 'Hello ' + name + ', great to see you!'; document.getElementById('mainDiv').style.display = "none"; } } 
 <div id="mainDiv"> <input type="text" class="form" name="name" placeholder="Your name here!" id="name"/> <input type="button" onclick="clicked();" value="I'm ready!"/> </div> <br> <div id="nameDiv"></div> 

  1. According to a similar question posted before there is no way to change the confirm dialogs button. 根据之前发布的类似问题,无法更改确认对话框按钮。
  2. I would suggest you can use bootstrap modal or jQueryUI. 我建议您可以使用引导模式或jQueryUI。

    There is even a workaround in the jQueryUI for this. jQueryUI甚至为此提供了一种解决方法。

  3. Or you can use bootstrap Modal. 或者您可以使用Bootstrap Modal。 Here is the link for it 这是它的链接

I hope my suggestions help with your problem. 希望我的建议对您的问题有所帮助。

You can do this 你可以这样做

function confirm_hide(ele){
    if (confirm('Do you wish to hide?')) {
        ele.style.display = 'none';
        document.getElementById('Mydiv').style.display = 'block';
        return true;
    } else {
        document.getElementById('Mydiv').style.display = 'none';
        return false;
    }
}

You can do like this also: 您也可以这样做:

 function confirm_hide(ele){ if(confirm('Do you wish to hide?')){ document.getElementById('Mydiv').style.display = 'block'; document.getElementById('mainDiv').style.display = 'none'; } } 
 <div id="Mydiv" style="display:none;" >Haiii</div> <div id="mainDiv"> <input type="button" name="answer" value="Show Div" onclick="confirm_hide()"/> </div> 

HTML 的HTML

<div id="Mydiv" style="display:none;" >Haiii</div>
<input type="button" name="answer" value="Show" class="confirm">

JS JS

var div    = document.getElementById("mydiv");
var button = document.getElementsByClassName("confirm");

button.addEventListener('click',confirm_hide());


function confirm_hide(){

var hide =  confirm('Do you wish to hide?');  

if(hide == true){
    button.style.display = 'none';
    div.style.display = 'block';
}
else{
      button.style.display = 'block';
      div.style.display = 'none';
    }


}

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

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