Jquery 如何在WordPress评论部分插入评级图像?
我需要在WordPress评论框中插入一个验证图像。如果评级为1/5、2/5、3/5、4/5,则应显示未验证图像,如果评级为5/5,则应显示验证图像。我尝试了很多版本,但是如果评分低于5,每次只能在所有评论框中显示一张图片 我的代码1:Jquery 如何在WordPress评论部分插入评级图像?,jquery,wordpress,Jquery,Wordpress,我需要在WordPress评论框中插入一个验证图像。如果评级为1/5、2/5、3/5、4/5,则应显示未验证图像,如果评级为5/5,则应显示验证图像。我尝试了很多版本,但是如果评分低于5,每次只能在所有评论框中显示一张图片 我的代码1: var code = $("<strong>5 / 5</strong>"); code.each(function() { $('p.comment-rating').prepend('<p class="prepen_i
var code = $("<strong>5 / 5</strong>");
code.each(function() {
$('p.comment-rating').prepend('<p class="prepen_img"><img src="verified.png" /></p>');
});
var code=$(“5/5”);
代码。每个(函数(){
$('p.comment-rating').prepend('p class=“prepen_img”>);
});
我的代码2:
if ($("p.comment-rating:contains('2 / 5')").length !== 0) {
$('p.comment-rating').prepend('<p class="prepen_img"><img src="not-verified.png" /></p>');
}
else {
$('p.comment-rating').prepend('<p class="prepen_img"><img src="verified.png" /></p>');
}
if($(“p.comment-rating:contains('2/5')))。长度!==0){
$('p.comment-rating').prepend('p class=“prepen_img”>);
}
否则{
$('p.comment-rating').prepend('p class=“prepen_img”>);
}
我的代码3:
$('p.comment-rating').each(function() {
if ($("p.comment-rating:contains('5 / 5')").length !== 0) {
$('p.comment-rating').prepend('<p class="prepen_img"><img src="not-verified.png" /></p>');
}
else {
$('p.comment-rating').prepend('<p class="prepen_img"><img src="verified.png" /></p>');
}
});
$('p.comment-rating')。每个(函数(){
如果($((“p.comment-rating:contains('5/5'))))。长度!==0){
$('p.comment-rating').prepend('p class=“prepen_img”>);
}
否则{
$('p.comment-rating').prepend('p class=“prepen_img”>);
}
});
HTML:
评级:2/5
上述代码仅显示一个图像,
否则
条件不起作用。因此类似的操作可能会起作用:
Html:
评级:2/5
评级:5/5
评级:1/5
jQuery:
$(function() {
$('.comment-rating').each(function() {
var $this = $(this),
// Finds the first number inside '.comment-rating strong' before the forward slash and converts it to a number.
rating = parseInt( $this.find('strong').text().split('/')[0] );
var verify = rating === 5 ? 'Verified' : 'Not-verified';
$this.append('<span>'+ verify +'</span>');
});
});
$(function() {
$('.comment-rating').each(function() {
var $this = $(this),
// Finds the first number inside '.comment-rating strong' before the forward slash and converts it to a number.
rating = parseInt( $this.find('strong').text().split('/')[0] );
var verify = rating === 5 ? 'http://placekitten.com/g/20/30' : 'http://placekitten.com/20/30';
$this.append('<img src="'+ verify +'" />');
});
});
$(函数(){
$('.comment rating')。每个(函数(){
变量$this=$(this),
//查找正斜杠前“.comment rating strong”中的第一个数字,并将其转换为数字。
rating=parseInt($this.find('strong').text().split('/')[0]);
var verify=评级===5?“已验证”:“未验证”;
$this.append(“”+验证+“”);
});
});
我想文本示例会更清晰,因为我没有这样的图像,但图像也是一样,我稍微修改了第9行和第11行: 同样的html jQuery:
$(function() {
$('.comment-rating').each(function() {
var $this = $(this),
// Finds the first number inside '.comment-rating strong' before the forward slash and converts it to a number.
rating = parseInt( $this.find('strong').text().split('/')[0] );
var verify = rating === 5 ? 'Verified' : 'Not-verified';
$this.append('<span>'+ verify +'</span>');
});
});
$(function() {
$('.comment-rating').each(function() {
var $this = $(this),
// Finds the first number inside '.comment-rating strong' before the forward slash and converts it to a number.
rating = parseInt( $this.find('strong').text().split('/')[0] );
var verify = rating === 5 ? 'http://placekitten.com/g/20/30' : 'http://placekitten.com/20/30';
$this.append('<img src="'+ verify +'" />');
});
});
$(函数(){
$('.comment rating')。每个(函数(){
变量$this=$(this),
//查找正斜杠前“.comment rating strong”中的第一个数字,并将其转换为数字。
rating=parseInt($this.find('strong').text().split('/')[0]);
var验证=额定值===5?'http://placekitten.com/g/20/30' : 'http://placekitten.com/20/30';
$this.append(“”);
});
});
你能帮助任何人吗?你在wordpress中有一个评级系统,你想在旁边显示未验证
或验证
图像,具体取决于评级?它是否输出html,如您在问题评分中所示:2/5
?@Joonas,绝对正确。那2/5是评级系统中最差的。哇。。你太棒了!难以置信的你变魔术了!。它工作得很好。非常感谢亲爱的乔纳斯再次感谢。。你能解释一下为什么我的代码不起作用吗?@KevinJohn Yea,我真的不知道为什么我一开始就丢弃了你的代码。无论如何,这个问题源于这样一个事实。就我个人而言,我似乎无法用简单的术语来解释这一点,因此我建议您使用console.log()例如,使用chrome开发者工具控制台查看它提供了什么值。例如,console.log($(this))
和console.log($('.comment rating')代码>感谢您的解释和建议