我在greasemonkey 用户脚本中使用jquery。我正在尝试将一堆选项添加到基于数组的选择中,并将相应的对象粘贴到带有 jquery.data 的元素中,如下所示:
$.each(some_array, function(item){
// These next 2 statements seem awkward to me and I was also hoping
// a jquery master could show me a slicker way to perhaps
// combine them into something simpler
$('select').append('<option>dummy</option>');
$('select option:last-child').data('obj', item);
});
然后我想在选择时恢复对象:
$('select').change(function(){
var theObj = $('option:selected', this).data('obj');
});
但是在我的greasemonkey 用户脚本中,theObj 是未定义的。我知道包装器,unsafeWindow 等有一些猴子生意。我只是希望有人可能知道究竟是什么让这不起作用。