gpt4 book ai didi

html - Angular 2 : css target selector not working

转载 作者:行者123 更新时间:2023-11-28 05:45:43 24 4
gpt4 key购买 nike

我正在尝试按照这个示例 ( http://www.webdesignerdepot.com/2012/10/creating-a-modal-window-with-html5-and-css3/ ) 在 Angular2 元素中创建我自己的模式。

在我的html模板中,我有

 <a href="#openModal" (click)="openDialog()" >Open Modal again</a>

<div id="openModal" class="modalDialog">
<div>
<a href="#close" title="Close" class="close">X</a>
<h2>Modal Box</h2>
<p>This is a sample modal box that can be created using the powers of CSS3.</p>
<p>You could do a lot of things here like have a pop-up ad that shows when your website loads, or create a login/register form for users.</p>
</div>
</div>

我添加 (click)="openDialog()" 的原因至 <a>是因为href="#openModal"没有注册到路由器。我必须阻止方法中的默认行为。

并且,在相应的 scss 文件中,我有

.modalDialog {
position: fixed;
font-family: Arial, Helvetica, sans-serif;
top: 0;
right: 0;
bottom: 0;
left: 0;
background: rgba(0,0,0,0.8);
z-index: 99999;
opacity:0;
-webkit-transition: opacity 400ms ease-in;
-moz-transition: opacity 400ms ease-in;
transition: opacity 400ms ease-in;
pointer-events: none;
}

.modalDialog:target {
opacity:1;
pointer-events: auto;
}

.modalDialog > div {
width: 400px;
position: relative;
margin: 10% auto;
padding: 5px 20px 13px 20px;
border-radius: 10px;
background: #fff;
background: -moz-linear-gradient(#fff, #999);
background: -webkit-linear-gradient(#fff, #999);
background: -o-linear-gradient(#fff, #999);
}

在组件的 ts 文件中,我有以下方法绑定(bind)到我的链接的点击事件。

openDialog(){
event.preventDefault();
}

上面的代码片段是在用户点击链接后弹出一个模式;然而,这样做之后什么也没有发生。我认为 css .modalDialog:target由于某些原因,选择器实际上不工作,因此不透明度没有改变。

最佳答案

通过调用 preventDefault(),您可以阻止该元素成为 #hash 的目标。

您必须删除 preventDefault() 调用才能将 #openModal 设置为点击时 URL 哈希的目标,并且使用 :target 选择器。

OP 表示这是有效的:

openDialog () {
location.hash = "#openModal"
event.preventDefault()
}

关于html - Angular 2 : css target selector not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37623303/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com