javascript - 在 jQuery 插件的选项部分访问调用者元素的属性

标签 javascript jquery colorbox

我有一些链接,根据它们在单击时显示的内容,它们具有不同的宽度和高度数据(此链接在服务器端生成),例如:

<a class="inline" href="#inline-content-1" data-width="80%" data-height="80%">content 1</a>
<a class="inline" href="#inline-content-2" data-width="50%" data-height="80%">content 2</a>

现在我想使用类似的东西:

$('.inline').colorbox({inline:true, width:$(this).data('width'), height:$(this).data('height')});

但是 $(this).data('width') 在选项部分似乎无效

最佳答案

Colorbox can be passed a function to be evaluated in place of a static value for any of its properties.

$('.inline').colorbox({
     inline: true, 
     width: function(){ return $(this).data('width'); }, 
     height: function(){ return $(this).data('height'); }
});

关于javascript - 在 jQuery 插件的选项部分访问调用者元素的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38223548/

相关文章:

jquery 迭代子元素

javascript - 如何在页面加载时打开一个宏伟的弹出窗口?

javascript - 当我单击搜索进行第二次搜索时,先前的搜索将被删除

javascript - 如何打开div内的颜色框

jquery - 将 Colorbox.js 标题移动到框顶部?

JavaScript 在 JSON 对象中递归搜索

javascript - 如何使用 d3 绘制动画折线图,同时需要每 1 秒更新一次数据表?

jquery - 提交后关闭 ColorBox iFrame

javascript - 使用 Tablesorter js 根据 Ajax 返回值更改行的背景

javascript - Nested_form_fields 在验证失败时显示现有的 has_many 嵌套字段