我有这个代码:
var li = $('<li>',{
html : '<div class="header">' + header + '</div><div class="subheader">'
+ sub_header + '</div><div class="optkey">' + option_key + '</div>'
});
var tmpLi = li.first()[0].firstChild;
if( active ) {
li.bind({
mousedown: function(event) {
console.log("mousedown");
event.stopPropagation();
select_box.find('.sbContent').html($(this).find('.header').html());
select_box_disabled.find('.sbContent').html($(this).find('.header').html());
select_options_container.trigger('hide');
// *FINISH* need to update the original select input with the new value
var key = $(this).find('.optkey').html();
select_source.find('option:selected').removeAttr('selected');
select_source.find('option:eq(' + key + ')').attr('selected', 'selected').change();
return false;
}
}),
tmpLi.bind({
keypress: function(event) {
console.log("keypress");
event.stopPropagation();
select_box.find('.sbContent').html($(this).find('.header').html());
select_box_disabled.find('.sbContent').html($(this).find('.header').html());
select_options_container.trigger('hide');
// *FINISH* need to update the original select input with the new value
var key = $(this).find('.optkey').html();
select_source.find('option:selected').removeAttr('selected');
select_source.find('option:eq(' + key + ')').attr('selected', 'selected').change();
return false;
}
});
}
在我添加tmpLi.bind
块之前,它工作正常。现在,我在 Firebug 中收到此错误:
类型错误:tmpLi.bind 不是函数
按键:功能(事件){
如何将唯一事件绑定到同一 if 语句中的两个元素?还是 tmpLi 不是有效元素的问题?(tmpLi 返回 html 字符串<div class="header">
)