【问题标题】:image id passing in find selector jquery issue传入查找选择器jquery问题的图像ID
【发布时间】:2018-10-30 17:48:08
【问题描述】:

我必须在 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 :::   "+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 客户标签中写了 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>");

【问题讨论】:

  • 你可以添加工作的sn-p :)
  • 你在crid得到什么(例如你得到img#picid0吗??)?
  • 我只是检查两者是否相同,我认为直接通过那里我正在通过 var。

标签: javascript jquery css image src


【解决方案1】:

请试试这个:

您的代码是正确的,我认为您的 image-id 不正确。请 看看吧。

请像这样添加:

$(this).parent().find('img#'+imageid).attr("src", "resources/vidal/images/accordion_down.svg");

【讨论】:

    【解决方案2】:

    您的代码很好,只是需要考虑的小事 在 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");
       });
    

    告诉我进展如何

    【讨论】:

      猜你喜欢
      • 2010-11-30
      • 1970-01-01
      • 2018-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-09
      相关资源
      最近更新 更多