Javascript JQuery 选择伪元素 :after
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27254674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JQuery select pseudo-element :after
提问by AiD
I want to select pseudo-element :after
我想选择伪元素 :after
This is my css :
这是我的 css:
show-more:after
{
content : (" > ");
}
JQuerycode
JQuery代码
$(function(){
$('.show-more:after').click(function() {
alert("Yes");
});
});
采纳答案by MartaGom
It's not possible to bind directly to pseudo-elements, since those are not part of the DOM, but the desired effect can be approximated by binding to a parent element and testing for an offset related to the element that the :afteracts upon:
不可能直接绑定到伪元素,因为它们不是 DOM 的一部分,但是可以通过绑定到父元素并测试与作用于的元素相关的偏移量来近似预期效果:after:
The following renders as ELEMENT++, where clicking on "ELEMENT" and "++" each triggers different behavior:
以下呈现为ELEMENT++,其中单击“ELEMENT”和“++”会触发不同的行为:
<span>ELEMENT</span>
span::after {
content: '++';
position: absolute;
}
span.c1 {
background: yellow;
}
span.c2::after {
background: orange;
}
const span = document.querySelector('span');
span.addEventListener('click', function (e) {
if (e.offsetX > span.offsetWidth) {
span.className = 'c2';
} else {
span.className = 'c1';
}
});
Interactive: http://jsfiddle.net/wC2p7/1/
回答by guest271314
Edit, Updated
编辑、更新
Try
尝试
(function($) {
jQuery.fn.extend({
getPseudo: function(pseudo, prop) {
var props = window.getComputedStyle(
$(this).get(0), pseudo
).getPropertyValue(prop);
return String(props);
},
setPseudo: function(_pseudo, _prop, newprop) {
var elem = $(this);
var s = $("style");
var p = elem.getPseudo(_pseudo, _prop);
var r = p !== "" ? new RegExp(p) : false;
var selector = $.map(elem, function(val, key) {
return [val.tagName
, val.id
? "#" + val.id : null
, val.className ? "." + val.className : null]
});
var _setProp = "\n" + selector.join("")
.toLowerCase()
.concat(_pseudo)
.concat("{")
.concat(_prop + ":")
.concat(newprop + "};");
return ((!!r ? r.test($(s).text()) : r)
? $(s).text(function(index, prop) {
return prop.replace(r, newprop)
})
: $(s).append(_setProp)
);
}
})
})(jQuery);
i.e.g., initial css:
例如,初始css:
.show-more:after {
content : ' > ';
}
set pseudoelement
设置pseudo元素
$(".show-more-after").on("click", function() {
$(this).setPseudo(":after", "content", "'123'")
})
github pseudo.js
github伪.js
(function($) {
jQuery.fn.extend({
getPseudo: function(pseudo, prop) {
var props = window.getComputedStyle(
$(this).get(0), pseudo
).getPropertyValue(prop);
return String(props);
},
setPseudo: function(_pseudo, _prop, newprop) {
var elem = $(this);
var s = $("style");
var p = elem.getPseudo(_pseudo, _prop);
var r = p !== "" ? new RegExp(p) : false;
var selector = $.map(elem, function(val, key) {
return [val.tagName
, val.id
? "#" + val.id : null
, val.className ? "." + val.className : null]
});
var _setProp = "\n" + selector.join("")
.toLowerCase()
.concat(_pseudo)
.concat("{")
.concat(_prop + ":")
.concat(newprop + "};");
return ((!!r ? r.test($(s).text()) : r)
? $(s).text(function(index, prop) {
return prop.replace(r, newprop)
})
: $(s).append(_setProp)
);
}
})
})(jQuery);
$(".show-more-after").on("click", function() {
$(this).setPseudo(":after", "content", "'123'");
})
.show-more-after:after {
content : ' > ';
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="show-more-after">click</div>
回答by Sleek Geek
Do something like this instead:
做这样的事情:
$(function(){
$('.show-more').after().click(function() {
alert("Yes");
});
});
.show-more {
Width: 100px;
Height: 40px;
Position: relative; /* Helps you define the area you want to be clickable */
Overflow: hidden;
}
.show-more:after {
content: 'Click me';
Left:0;
Top: 0;
width: 100%;
height: 100%;
background: red;
color: white;
padding: 15px;
line-height: 40px;
cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="show-more"> </div>
回答by A.B
use this to add text after show more
在显示更多内容后使用它来添加文本
$(function(){
$('.show-more').text($('.show-more').text()+">");
});

