【问题标题】:How to create a pre-choice selection?如何创建预选选择?
【发布时间】:2017-05-09 12:07:45
【问题描述】:

我有兴趣在页面前创建一个临时屏幕,让人们可以选择一个选项来确定他们显示的内容。

示例:屏幕询问“您是否有兴趣就购买事宜与我们联系或与我们的客户团队交谈”的联系页面。一旦他们点击一个选项,它将显示相应的联系表格(一个给客户团队发送电子邮件,另一个给采购团队发送电子邮件)。

由于我是 HTML/CSS/JavaScript 的新手,我不确定如何编写此代码,但根据我目前所学的知识,我假设这是更高级的 JavaScript。

【问题讨论】:

  • 你能附上你的代码吗?
  • 不,我不知道如何编码。
  • 那么这个问题应该被关闭为“太宽泛”。请阅读How to Ask
  • 我在寻求提示@PatrickHund 如果我不知道该怎么做,我该如何发布代码?只是可能围绕这个想法的主题或想法,以便我可以阅读并遵循它。
  • 提示?好的:阅读事件侦听器(单击或更改单选按钮或下拉列表的事件,或者您想要显示的选项),并阅读hiding/showing elements from JS by manipulating the CSS.

标签: javascript html css


【解决方案1】:

我可以提供一个简单的示例让您了解它,但我建议您先进行更多基本的前端培训。

if (window.confirm('Do you want load form a?')) {
  document.getElementById('a').style.display = 'block';
} else {
  document.getElementById('b').style.display = 'block';
}

https://jsfiddle.net/8LmL4vdr/1/

【讨论】:

  • 我同意你的建议。谢谢你!将对此进行试验。
【解决方案2】:

我不完全确定您在问什么,但听起来如果您在单击按钮时更改元素的 z-index 值,它会起作用。

您可以阅读有关 z-index 的更多信息here

或者,您可以使用display

这是一个例子:

function layerPages(page) {
    if (page == "one") {
         document.getElementById("pageOne").style.display = "block"; // shows first page if that is what is called
    } else if (page == "two") {
         document.getElementById("pageTwo").style.display = "block"; // shows second page if that is what is called
    } else {
    	window.alert("error, wrong useage");
    }
    document.getElementById("originalPage").style.display = "none"; // hides main page
}

document.getElementById("buttonOne").addEventListener("click", function 
myfunction() {layerPages("one");}); // adds event listener that states that when the first button is clicked, the first page is shown and the original is hidden using the function "layerPages" (declared above)
document.getElementById("buttonTwo").addEventListener("click", function 
myfunction() {layerPages("two");}); // adds event listener that states that when the second button is clicked, the second page is shown and the original is hidden using the function "layerPages" (declared above)
#originalPage {
  background-color: black;
  width: 100px;
  height: 100px;
}
#pageOne, #pageTwo {
  display: none; /*You need to make sure that both #pageOne and #pageTwo start out as display: none;*/
  background-color: yellow;
  width: 100px;
  height: 100px;
}
#pageTwo {
  background-color: orange;
}
<div id="originalPage">
<button id="buttonOne">Page One</button>
<button id="buttonTwo">Page Two</button>
</div>
<div id="pageOne"></div>
<div id="pageTwo"></div>

<!--The content goes into each div, I'm sure that you can figure that out-->

【讨论】:

  • 但是你不得不担心定位和不透明度等。只是摆弄style.display属性不是更简单吗?
  • 非常感谢,z-index 对我来说是新的,但我会阅读它的用法。也谢谢你的 sn-p。
猜你喜欢
  • 1970-01-01
  • 2016-11-28
  • 1970-01-01
  • 2014-11-18
  • 1970-01-01
  • 2021-06-01
  • 2020-10-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多