简体   繁体   English

从jQuery 1.5.1升级选择元素以使用jQuery 1.9.1?

[英]Upgrade select element from jQuery 1.5.1 to work with jQuery 1.9.1?

I am trying to use a select form-element that has been made using jQuery 1.5.1 but the environment that I'm using it in is setup to use jQuery 1.9.1 and my select box completely disappears. 我正在尝试使用通过jQuery 1.5.1制作的选择表单元素,但是我在其中使用的环境设置为使用jQuery 1.9.1,并且我的选择框完全消失了。 How can I change this code to work with 1.9.1? 如何更改此代码以使其与1.9.1一起使用?

Change the linked jQuery library version number from 1.5.1 to 1.9.1 to see exactly what I mean. 将链接的jQuery库版本号从1.5.1更改为1.9.1,以准确了解我的意思。

Fiddle: http://jsfiddle.net/BinaryAcid/6n2tn/1/ Pen: http://codepen.io/Justice-Conder/pen/uBIhy 小提琴: http : //jsfiddle.net/BinaryAcid/6n2tn/1/笔: http : //codepen.io/Justice-Conder/pen/uBIhy

$(document).ready(function() {
var select = $('select.prettyfied');

var selectBoxContainer = $('<div>',{
width     : select.outerWidth(),
className : 'prettyfied-select',
html      : '<div class="prettyfied-select-box"><span></span></div>'
});

var dropDown = $('<ul>',{className:'dropDown'});
var selectBox = selectBoxContainer.find('.prettyfied-select-box');

// Looping though options of original select element
select.find('option').each(function(i) {
var option = $(this);
if(i == select.attr('selectedIndex')) {
  selectBox.html('<span>'+option.text()+'</span>');
}

// Access HTML5 data attributes with the data method
if(!option.data('html-text')) {
  return true;
}

// Create dropdown item according to data-icon & data-html-text attributes
var li = $('<li>',{
  html: '<span>' + option.data('html-text') + '</span>'
});

li.click(function() {
  selectBox.html('<span>'+option.text()+'</span>');
  dropDown.trigger('hide');

// When click occurs, we reflect change on original select element
  select.val(option.val());

  return false;
}).hover(function() {
  $(this).addClass('hover');
}, function() {
  $(this).removeClass('hover');
});

dropDown.append(li);
});

selectBoxContainer.append(dropDown.hide());
select.hide().after(selectBoxContainer);

// Binding custom show/hide events on dropDown
dropDown.bind('show',function(){
if(dropDown.is(':animated')){
  return false;
}
selectBox.addClass('expanded');
dropDown.slideDown();
}).bind('hide',function(){
if(dropDown.is(':animated')){
  return false;
}
selectBox.removeClass('expanded');
dropDown.addClass('is-hidden');
dropDown.slideUp(function() {
  dropDown.removeClass('is-hidden');
});
}).bind('toggle',function() {
if(selectBox.hasClass('expanded')) {
  dropDown.trigger('hide');
}
else dropDown.trigger('show');
});

selectBox.click(function() {
dropDown.trigger('toggle');
return false;
});

// Click on page, while the dropdown is shown, to close it
$(document).click(function(){
dropDown.trigger('hide');
});
});

Best feedback I'v received so far: 到目前为止,我收到的最佳反馈是:

  1. Use class instead of className when creating the DOM object. 创建DOM对象时,请使用class而不是className。
  2. Use select.prop() instead of select.attr() when getting the index of the selected option. 获取选定选项的索引时,请使用select.prop()而不是select.attr()。

Thanks for all the help! 感谢您的所有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM