我有一个 javascript 代码将 jQuery 颜色框绑定(bind)到我的元素,但无法弄清楚如何更改回调内属性的值:
$(".iframe").colorbox({
iframe: true,
width: '80%',
height: '80%',
onLoad: function() {
console.log(width);
},
});
为什么我会收到 ReferenceError: width is not Defined
或者如何访问此属性?
最佳答案
您会收到错误,因为宽度只是您传递给 colorbox 函数的对象的一个属性。它不是您可以引用的变量。
他们这样做:
var myWidth = '80%';
$(".iframe").colorbox ({
iframe:true,
width: myWidth,
height:'80%',
onLoad:function() {
console.log( myWidth );
},
});
或者这个
var props = {
iframe:true,
width: '80%',
height:'80%',
onLoad:function() {
console.log( props.width );
},
};
$(".iframe").colorbox(props);
关于javascript - 无法访问 JavaScript 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24744326/