Get attributes from the rel attribute with jQuery

你说的曾经没有我的故事 提交于 2019-12-02 08:27:18

问题


Im using a plugin that requires the rel-element to look like this.

<ul id="product-thumbs-list">
      <li><a href="1-big.jpg" rel="useZoom: 'cdonZoom', smallImage: '1.jpg'"></li>
      <li><a href="2-big.jpg" rel="useZoom: 'cdonZoom', smallImage: '2.jpg'"></li>
</ul>

Is it possible to get the smallImage-value via jQuery?
In this case, '1.jpg, or '2.jpg'.

Thanks!


回答1:


Here is one way:

$("#product-thumbs-list a").each(function(index) {
   var arrTemp = $(this).attr("rel").split("smallImage: ");
   var value = arrTemp[1];
   alert(value);
});

Live test case.




回答2:


you can get the rel attributes that returns the string "useZoom: 'cdonZoom', smallImage: '1.jpg'". Than you can split the string using ":" and get the last array item.

I hope it's helpful




回答3:


Superbly ugly, but you can use the dreaded eval(), provided that the rel data are "well-formed":

$(function() {
    $('a').mouseover(function() {
        eval('var rel = {' + $(this).attr('rel') + '};');
        $('#out').text(rel.smallImage);
    });
});

With HTML:

<ul id="product-thumbs-list">
    <li><a href="1-big.jpg" rel="useZoom: 'cdonZoom', smallImage: '1.jpg'">link1</a></li>
    <li><a href="2-big.jpg" rel="useZoom: 'cdonZoom', smallImage: '2.jpg'">link2</a></li>
</ul>
<p id="out"></p>

Demo.



来源:https://stackoverflow.com/questions/5470216/get-attributes-from-the-rel-attribute-with-jquery

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!