首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为父属性中的多个元素添加一个前缀

为父属性中的多个元素添加一个前缀
EN

Stack Overflow用户
提问于 2018-02-24 21:42:41
回答 1查看 82关注 0票数 0

我试图弄清楚如何在ID和其他引用链接中添加一个唯一的前缀。

一个页面上有多个相同的SVG。这些SVG充当包装器(它们是设备--笔记本电脑、电话等)其中有图像链接,加载后插入到SVG中。问题是,这些svg的ID是相同的,所以它们都相互冲突。我想要做的(但对更好的解决方案开放)是为每个idxlink:hrefurl(#等插入一个唯一的ID,但是传递<image> href属性以及fillstroke属性。

编辑:我添加/调整了@Temani提供的代码,这使我更接近我想要的结果,但是,我现在正在将所有ID添加到每个SVG元素中。格式化要可执行的代码。

期望输出:

代码语言:javascript
运行
复制
<div class="device" data-screen="[[IMAGE TO USE]]" data-name"[[UNIQUE ID]]">
  <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="635" height="420" viewBox="0 0 635 420">
    <defs>
      <path id="[[UNIQUE ID]]-path-1"/>
      <rect id="[[UNIQUE ID]]-path-3" />
      <pattern id="[[UNIQUE ID]]-pattern-4">
        <use transform="scale(13.1875)" xlink:href="#[[UNIQUE ID]]-image-5"/>
      </pattern>
      <image id="[[UNIQUE ID]]-image-5" href="[[IMAGE LINK THAT IS INSERTED AFTER LOAD]]"/>
    </defs>
    <g fill="none" fill-rule="evenodd">
      <rect fill="#fff" stroke="#2D8EFF" />
      <g >
        <mask id="[[UNIQUE ID]]-mask-2" fill="#fff">
          <use xlink:href="#[[UNIQUE ID]]-path-1"/>
        </mask>
        <g mask="url(#[[UNIQUE ID]]-mask-2)">
          <mask id="mask-6" fill="#fff">
            <use xlink:href="#[[UNIQUE ID]]-path-3"/>
          </mask>
        </g>
      </g>
    </g>
  </svg>
</div>

现在-两个ID(第一个和第二个)被添加到每个SVG元素中。我也

代码语言:javascript
运行
复制
$('.device').each(function() {
     //we get the needed id using $(this) that refer to actual device
     var id = $(this).data('name');

    //we check all the element with ID
    $(this).find("path, rect, pattern, image, mask").each(function() {
      //now (this) refer to the actual element
      if($(this).attr("id"))
        $(this).attr("id", id+"-"+$(this).attr("id"));
    });
    //we update the <use>
     $(this).find("use").each(function() {
      //now (this) refer to the actual element
     $(this).attr("xlink:href","#"+id+"-"+$(this).attr("xlink:href").substring(1));
    });
    

// Also, hoping to combine g[mask], u[fill], etc into 1 function. 
// Basically any attribute that starts with "url(#"
    $(this).find("g[mask^='url']").each(function() {
     
$(this).attr("mask","url(#"+id+"-"+$(this).attr("mask").substring(5));
       });
  
  	$(this).find("use[fill^='url']").each(function() {
         $(this).attr("fill","url(#"+id+"-"+$(this).attr("fill").substring(5));
       });
       
       $(this).find("use[filter^='url']").each(function() {
         $(this).attr("filter","url(#"+id+"-"+$(this).attr("filter").substring(5));
       });
    // grab data-screen value
    var data = $(this).data('screen');

    // replace this with the link inside svg
    if (data != '') {
      $(this).find("svg defs image").attr("href", data).attr("xlink:href", data);
    }
    }); 
代码语言:javascript
运行
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="device" data-name="first" data-screen="image-1.png">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 24 24">
  <defs>
    <rect id="w-circle-stroke-a" width="24" height="24"/>
    <path id="w-circle-stroke-b" d="M12,2 C17.52,2 22,6.48 22,12 C22,17.52 17.52,22 12,22 C6.48,22 2,17.52 2,12 C2,6.48 6.48,2 12,2 Z M12,3.81818182 C7.48415409,3.81818182 3.81818182,7.48415409 3.81818182,12 C3.81818182,16.5158459 7.48415409,20.1818182 12,20.1818182 C16.5158459,20.1818182 20.1818182,16.5158459 20.1818182,12 C20.1818182,7.48415409 16.5158459,3.81818182 12,3.81818182 Z M10.5553177,13.4773237 L15.155405,8.80967806 C15.5597962,8.4027095 16.222261,8.39598875 16.6350615,8.79466684 C16.6382917,8.79778661 16.6600317,8.81952282 16.7002813,8.85987545 C17.0999062,9.26113743 17.0999062,9.90402237 16.7002813,10.3052843 L10.5553177,16.5 L7.29971874,13.2228714 C6.90252847,12.8240541 6.8997633,12.1859262 7.29348277,11.7837778 L7.33224151,11.7441893 C7.73340831,11.3344341 8.39555055,11.3228774 8.8111776,11.7183766 C8.81566955,11.722651 9.39704957,12.3089667 10.5553177,13.4773237 Z"/>
    <image id="image-5" href="image-to-be-inserted.jpg"/>
  </defs>
  <g fill="none" fill-rule="evenodd">
    <mask id="w-circle-stroke-c" fill="#fff">
      <use xlink:href="#w-circle-stroke-b"/>
    </mask>
    <g fill="#2D8EFF" mask="url(#w-circle-stroke-c)">
      <rect width="24" height="24"/>
    </g>
    <use fill="url(#pattern-4)" xlink:href="#path-3"/>
    <use fill="#000" filter="url(#filter-10)" xlink:href="#path-9"/>
  </g>
</svg>
</div>

<div class="device" data-name="second" data-screen="image-1.png">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 24 24">
  <defs>
    <rect id="w-circle-stroke-a" width="24" height="24"/>
    <path id="w-circle-stroke-b" d="M12,2 C17.52,2 22,6.48 22,12 C22,17.52 17.52,22 12,22 C6.48,22 2,17.52 2,12 C2,6.48 6.48,2 12,2 Z M12,3.81818182 C7.48415409,3.81818182 3.81818182,7.48415409 3.81818182,12 C3.81818182,16.5158459 7.48415409,20.1818182 12,20.1818182 C16.5158459,20.1818182 20.1818182,16.5158459 20.1818182,12 C20.1818182,7.48415409 16.5158459,3.81818182 12,3.81818182 Z M10.5553177,13.4773237 L15.155405,8.80967806 C15.5597962,8.4027095 16.222261,8.39598875 16.6350615,8.79466684 C16.6382917,8.79778661 16.6600317,8.81952282 16.7002813,8.85987545 C17.0999062,9.26113743 17.0999062,9.90402237 16.7002813,10.3052843 L10.5553177,16.5 L7.29971874,13.2228714 C6.90252847,12.8240541 6.8997633,12.1859262 7.29348277,11.7837778 L7.33224151,11.7441893 C7.73340831,11.3344341 8.39555055,11.3228774 8.8111776,11.7183766 C8.81566955,11.722651 9.39704957,12.3089667 10.5553177,13.4773237 Z"/>
    <image id="image-5" href="image-to-be-inserted.jpg"/>
  </defs>
  <g fill="none" fill-rule="evenodd">
    <mask id="w-circle-stroke-c" fill="#fff">
      <use xlink:href="#w-circle-stroke-b"/>
    </mask>
    <g fill="#2D8EFF" mask="url(#w-circle-stroke-c)">
      <rect width="24" height="24"/>
    </g>
    <use fill="url(#pattern-4)" xlink:href="#path-3"/>
    <use fill="#000" filter="url(#filter-10)" xlink:href="#path-9"/>
  </g>
</svg>
</div>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-24 23:05:56

问题是,您不需要在内部使用$('.device'),您需要引用$(this),并且由于您有2个each()嵌套,所以需要注意this的范围。

代码语言:javascript
运行
复制
$('.device').each(function() {
   //we get the needed id using $(this) that refer to actual device
   var id = $(this).data('name');
    
  //we check all the element with ID
  $(this).find("path, rect, pattern, image, mask").each(function() {
    //now (this) refer to the actual element
    if($(this).attr("id"))
      $(this).attr("id", id+"-"+$(this).attr("id"));
  });
  //we update the <use>
   $(this).find("use").each(function() {
    //now (this) refer to the actual element
    $(this).attr("xlink:href","#"+id+"-"+$(this).attr("xlink:href").substring(1));
  });

  // grab data-screen value
  var data = $(this).data('screen');

  // replace this with the link inside svg
  if (data != '') {
    $(this).find("svg defs image").attr("href", data).attr("xlink:href", data);
  }
});
代码语言:javascript
运行
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="device" data-screen="[[IMAGE TO USE]]" data-name="[[UNIQUE ID]]">
  <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="635" height="420" viewBox="0 0 635 420">
    <defs>
      <path id="path-1"/>
      <rect id="path-3" />
      <pattern id="pattern-4">
        <use transform="scale(13.1875)" xlink:href="#image-5"/>
      </pattern>
      <image id="image-5" href="[[IMAGE LINK THAT IS INSERTED AFTER LOAD]]"/>
    </defs>
    <g fill="none" fill-rule="evenodd">
      <rect fill="#fff" stroke="#2D8EFF" />
      <g >
        <mask id="mask-2" fill="#fff">
          <use xlink:href="#path-1"/>
        </mask>
        <g mask="url(#mask-2)">
          <mask id="mask-6" fill="#fff">
            <use xlink:href="#path-3"/>
          </mask>
        </g>
      </g>
    </g>
  </svg>
</div>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48967929

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档