巧克力js从链接attr href打开图像

我想用巧克力js打开图像,但使用href属性来传递url。


从巧克力的例子中得到这个:


这有效


超文本标记语言


<a href="https://picsum.photos/1000/1000">click</a>

JS


const images = [

    { 

    src: 'https://picsum.photos/1000/1000', title: 'image caption a' 

    }

]


const { api } = Chocolat(images, {})


$('a').on('click', function(e) {

  var url = $(this).attr('href');

  api.open();

  e.preventDefault();

});

我想要巧克力从我的链接中获取 var url,而不是 const 图像。


小提琴


哆啦的时光机
浏览 147回答 1
1回答

弑天下

这可以解决问题:JS$('a').on('click', function(e) {&nbsp; &nbsp; &nbsp; &nbsp; var url = $(this).attr('href');&nbsp; &nbsp; &nbsp; &nbsp; const images = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { src: url },&nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; &nbsp; if (window.chocolatInstance) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; window.chocolatInstance.close();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; window.chocolatInstance = Chocolat(images, {}).api&nbsp; &nbsp; &nbsp; &nbsp; chocolatInstance.open();&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; e.preventDefault();&nbsp; &nbsp; });
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript