Javascript 在jquery中更改图像?
我有这个jquery代码,它工作得很好,但是最后的图像并没有改变为我在这里指定的src: jquery:Javascript 在jquery中更改图像?,javascript,jquery,image,src,Javascript,Jquery,Image,Src,我有这个jquery代码,它工作得很好,但是最后的图像并没有改变为我在这里指定的src: jquery: $(document).ready( function() { $("a.vote_up").click(function(){ //get the id var the_id = $(this).attr('id'); //the main ajax request $.ajax( {
$(document).ready( function() {
$("a.vote_up").click(function(){
//get the id
var the_id = $(this).attr('id');
//the main ajax request
$.ajax( {
type: "POST",
data: "action=vote_up&id=" + the_id,
url: "ajax/votes.php",
success: function( msg ) {
$("span.vote_count#"+the_id).html(msg).fadeIn();
// my problem is here
$(".vote_up#" + the_id + " img").attr("src", "img/upvoteActive.png");
}
} );
} );
} );
html代码:
<a href='#' class='vote_up' id="$id"><img src="img/uparrow.png" /></a>
不要将class与ID结合使用;它是多余的,因为ID应该总是唯一的
$("#" + the_id + " img").attr("src", "img/upvoteActive.png");
此外,不能在ID属性中使用字符$。引用
ID和名称标记必须以
字母([A-Za-z]),并可遵循
通过任意数量的字母、数字
([0-9]),连字符(“-”),下划线
(“)、冒号(“:”)和句点
(“)
看起来您多次使用相同的ID(img和count)。尝试使ID更独特,如:
<a href='#' class='vote_up' id="$id_link"><img src="img/uparrow.png" /></a>
<span class="vote_count" id="$id_count"></span>
+1;只是因为它对你不起作用,所以没有必要否决投票,因为答案仍然很有道理。但是它不相关,他应该使用评论系统,它不是答案,它的建议@逃跑这不是无关紧要的,这是正确的答案。读第一句话。ID应始终是唯一的。这是一个答案,而不是建议,我现在将把你的用户名放在“不需要帮助的人”类别中。你不能让它变得“更独特”。它要么是独一无二的。。。或者不是。