Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 使用$(this)关键字访问伪元素?_Jquery_Pseudo Element - Fatal编程技术网

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用于解决方案