![](/img/trans.png)
[英]How to simultaneously select adjacent element before and after it using jquery?
[英]how to select :after element using jquery
贝娄是我的代码。我尝试过。当弹出这个弹出窗口时,我想用这个关闭按钮来关闭整个popbox。
CSS代码
.bigdiv{
display:none;
background-color:#efefef;
box-shadow: 10px 10px 10px 100000px rgba(0, 0, 0, 0.4);
border:2px solid #efefef;
width:400px;
height:300px;
position:fixed;
z-index:500;
top:25%;
left:25%;
margin:0 auto;
padding:20px;
}
.bigdiv:after {
cursor:pointer;
content:url('http://static.doers.lk/img/closebox.png');
position: relative;
right: -195px;
top: -310px;
z-index: 999;
}
JQUERY
$(".left div").click(function () {
$(".bigdiv").show("slow");
$(".bigdiv").click(function () {
$(".bigdiv").hide();
}) ; }) ;
HTML
<div class="left">
<div>intro text here</div><div></div><div></div>
</div>
<div class="bigdiv">some content</div>
我想选择:在元素之后。如何使用jquery做到这一点?
我想选择:在元素之后。如何使用jquery做到这一点?
你不能,DOM中不存在生成的伪元素(但遗憾的是)。
我们可以证明这样:
CSS:
#foo:before {
content: '[Before]'
}
#foo:after {
content: '[After]'
}
HTML:
<body><div id="foo">Foo</div></body>
JavaScript的:
(function() {
var msgs = [];
var child;
var div;
for (child = document.body.firstChild;
child;
child = child.nextSibling) {
if (child.id) {
msgs.push("Child " + child.nodeName + "#" + child.id);
}
else {
msgs.push("Child " + child.nodeName);
}
}
div = document.createElement('div');
div.innerHTML = msgs.join("<br>");
document.body.appendChild(div);
})();
上面得到的页面(如果我们假设script
元素最后添加到body
)是:
[Before]Foo[After] Child DIV#foo Child SCRIPT
如您所见,就DOM而言,生成的伪元素根本不存在。
您可以使用getComputedStyle函数,该函数受大多数主流浏览器支持 - IE9,IE10,Chrome,FF。 我没有找到在IE8中这样做的方法。
var attrContent = getComputedStyle(this,':after').content;
var attrWidth = getComputedStyle(this,':after').width;
如果您发现此函数未定义,请尝试使用window.getComputedStyle。
不幸的是,你不能在jQuery(或一般的JavaScript)中选择伪元素。
jQuery的appendTo()
方法与CSS完全相同:after
,它可以作为替代品(虽然不如纯CSS解决方案那么优雅)。
例如,而不是:
.bigdiv:after {
cursor:pointer;
content:url('http://static.doers.lk/img/closebox.png');
position: relative;
right: -195px;
top: -310px;
z-index: 999;
}
试试吧
CSS:
.bigdivAfter {
cursor:pointer;
position: relative;
right: -195px;
top: -310px;
z-index: 999;
}
JS:
$("<div class=bigdivAfter><img src='http://static.doers.lk/img/closebox.png'></div>").appendTo(".bigdiv");
然后您可以正常选择密码箱图像。
你可以使用jquery追加关闭按钮,并可以为它分配一个关闭事件。 这个jquery和css对你有用。
CSS
.bigdivAfter {
cursor:pointer;
position: absolute;
right: //as you want;
top: //as you want;
z-index: 999;
}
JQUERY
$(document).ready(function(){
$(".bigdiv").append('<img class="closeButton" src="http://static.doers.lk/img/closebox.png"/>');
$(".bigdiv .closeButton").click(function () {
$(".bigdiv").hide();
}) ;
$(".left div").click(function () {
$(".bigdiv").show("slow");
}) ;
我还建议你在html上添加图像,而不是通过jquery添加它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.