Jquery 使用$(this)关键字访问伪元素?
这是我的密码Jquery 使用$(this)关键字访问伪元素?,jquery,pseudo-element,Jquery,Pseudo Element,这是我的密码 var pollPercent = function() { $("#poll li").each(function(){ var percent = $(this).attr("data-percent"); var width = (percent / 100) * 1000; var el = $(this); $("head").append("<style>
var pollPercent = function() {
$("#poll li").each(function(){
var percent = $(this).attr("data-percent");
var width = (percent / 100) * 1000;
var el = $(this);
$("head").append("<style>" + el + ":before {width: " + width + "px;}</style>");
});
};
pollPercent();
var pollPercent=函数(){
$(“#poll li”)。每个(函数(){
变量百分比=$(此).attr(“数据百分比”);
变量宽度=(百分比/100)*1000;
var el=$(本);
$(“head”).append(“+el+”:在{width:“+width+”px;})之前;
});
};
花粉百分比();
因此,我如何指向每个li并更改其“before伪元素”您可以使用
n子元素
获取单个li
元素。每个
函数都有助于提供索引
$(...).each(function(index, element) {
... "li:nth-child(" + index + "):before" ...
})
试试这个:
var text = $('#poll').find('li').map(function (i) {
var width = Math.round($(this).attr('data-percent') / 100 * 1000);
return '#poll li:nth-child(' + (i+1) + ')::before { width: ' + width + 'px; }';
}).join('');
$(document.body).append('<style>' + text + '</style>');
var text=$('#poll').find('li').map(函数(i){
var width=Math.round($(this.attr('data-percent'))/100*1000);
在{width:'+width+'px;}之前返回'#poll li:n第n个子('+(i+1)+'):';
}).加入(“”);
$(document.body).append(“”+text+“”);
tanx用于解决方案