Javascript 图像id传入查找选择器jquery问题
我必须在find()选择器中动态地传递一些值来更改图像,所以我尝试如下传递Javascript 图像id传入查找选择器jquery问题,javascript,jquery,css,image,src,Javascript,Jquery,Css,Image,Src,我必须在find()选择器中动态地传递一些值来更改图像,所以我尝试如下传递 $('.vd-accordion-grid .collapse').on('shown.bs.collapse', function() { var imageid=$(this).parent().find("img").attr("id"); var crid='img#'+imageid; alert("image id ::: "+imagei
$('.vd-accordion-grid .collapse').on('shown.bs.collapse', function() {
var imageid=$(this).parent().find("img").attr("id");
var crid='img#'+imageid;
alert("image id ::: "+imageid);
alert("crid:: "+crid);
$(this).parent().find(crid).attr("src", "resources/vidal/images/accordion_down.svg");
}).on('hidden.bs.collapse', function() {
$(this).parent().find(crid).attr("src", "resources/vidal/images/accordion_right.svg");
});
但图像并没有改变,但当我在find中硬编码图像的id时,图像成功地改变了
$(this).parent().find('img#picid0').attr("src", "resources/vidal/images/accordion_down.svg");
实际上,我已经用java custome标记编写了html代码,请在下面找到
"<a data-toggle=\"collapse\" href=\"#collapse"+iClauseCnt+"\" aria-expanded=\"true\" aria-controls=\"collapse"+iClauseCnt+"\" class=\"\" style=\"color:white;font-size: 15px;\">");
"<img id=\"picid"+iClauseCnt+"\" src=\""+webcontext+"/resources/vidal/images/accordion_down.svg\" alt=\"accordion_down\">");
""+eleClause.valueOf("@name"));
"</a>");
”);
请尝试以下操作:
你的代码是对的,我认为,你没有正确的图像id。请
看看吧
请添加如下内容:
$(this).parent().find('img#'+imageid).attr("src", "resources/vidal/images/accordion_down.svg");
<>你的代码很好,只是一个小问题。 在on('hidden.bs.collapse')回调函数中,变量crid“未定义”,因为它未在此函数的作用域中声明 检查下面的代码
$('.vd-accordion-grid .collapse').on('shown.bs.collapse', function() {
var imageid=$(this).parent().find("img").attr("id");
var crid='img#'+imageid;
alert("image id ::: "+imageid);
alert("crid:: "+crid);
$(this).parent().find(crid).attr("src", "resources/vidal/images/accordion_down.svg");
}).on('hidden.bs.collapse', function() {
// crid here was undefined <======================
var imageid=$(this).parent().find("img").attr("id");
var crid='img#'+imageid;
$(this).parent().find(crid).attr("src", "resources/vidal/images/accordion_right.svg");
});
$('.vd accordion grid.collapse').on('show.bs.collapse',function()){
var imageid=$(this.parent().find(“img”).attr(“id”);
var crid='img#'+imageid;
警报(“图像id::”+imageid);
警报(“crid::”+crid);
$(this.parent().find(crid.attr)(“src”,“resources/vidal/images/accordion_down.svg”);
}).on('hidden.bs.collapse',function(){
//这里的crid是未定义的,你能添加工作片段吗:)你在crid
中得到了什么(比如你得到了img#picid0
)?我只是在检查两者是否相同,我想我是在直接通过var。