13

贝娄是我的代码。我尝试过的。当这个弹出窗口出现时,我想使用这个关闭按钮来关闭整个弹出框。

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;
    }

查询

$(".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 做到这一点?

4

4 回答 4

12

我想选择:元素之后。如何使用 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最后)是:

[之前]Foo[之后]
子 DIV#foo
子脚本

实时复制| 来源

如您所见,就 DOM 而言,生成的伪元素根本不存在。

于 2012-12-22T08:44:43.193 回答
3

您可以使用 getComputedStyle 功能,大多数主流浏览器 - IE9、IE10、Chrome、FF 都支持该功能。不过,我还没有找到在 IE8 中执行此操作的方法。

var attrContent = getComputedStyle(this,':after').content;
var attrWidth = getComputedStyle(this,':after').width;

如果您发现此函数未定义,请尝试使用 window.getComputedStyle。

于 2013-11-09T23:42:48.533 回答
0

不幸的是,您不能在 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");

然后您可以正常选择closebox图像。

于 2012-12-22T08:47:08.500 回答
0

您可以使用 jquery 附加关闭按钮,并可以为其分配关闭事件。这个 jquery 和 css 将为您工作。

CSS

.bigdivAfter {
    cursor:pointer;
    position: absolute;
    right: //as you want;
    top: //as you want;
    z-index: 999;
}

查询

$(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 添加它

于 2012-12-22T10:02:40.933 回答