How do I link a hyperlink to an HTML5 <li> image that uses data attribues

淺唱寂寞╮ 提交于 2019-12-25 17:04:28

问题


This is a responsive HTML5 gallery that I've been working on recently. I just want each image to link to a different page.

<li> 

<div data-alt="img03" data-description="<h3>Project</h3>" data-max-width="1800" data-max-height="1350" > 
<div data-src="images/xxxlarge/1.jpg" data-min-width="1300"></div>


</div>

</li>

 I know that it should be as simple as adding a <a href="#"></a> but this isn't working. I am open to hear about other options. Thanks.

回答1:


You can access the values in data-attributes (data-*) using .dataset (currently supported in all major browsers). I.e. to access the value of the attribute data-src of an element, you would write:

var srcValue = varHoldingElemNode.dataset.src;   // or
var srcValue = varHoldingElemNode.dataset["src"];

That said, you can use some JS to achieve what (I suppose) you want: turning a <div data-src=<url_to_image>... into an image linking to some page.

See, also, this short demo.



来源:https://stackoverflow.com/questions/16594265/how-do-i-link-a-hyperlink-to-an-html5-li-image-that-uses-data-attribues

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