获取子元素的ID并附加到父元素
我有一个函数可以从选择框中创建 HTML 元素以允许样式设置。代码如下:
$.fn.jqTransSelect = function () {
return this.each(function (index) {
var $select = $(this);
if ($select.hasClass('jqTransformHidden')) {
return;
}
if ($select.attr('multiple')) {
return;
}
var oLabel = jqTransformGetLabel($select);
var $wrapper = $select.addClass('jqTransformHidden').wrap('<div class="jqTransformSelectWrapper"></div>').parent().css({
zIndex: 10 - index
});
$wrapper.prepend('<div><span></span><a href="#" class="jqTransformSelectOpen"></a></div><ul></ul>');
var $ul = $('ul', $wrapper).css('width', $select.width() + 10).hide();
$('option', this).each(function (i) {
var oLi = $('<li><a href="#" index="' + i + '">' + $(this).html() + '</a></li>');
$ul.append(oLi);
});
$ul.find('a').click(function () {
$('a.selected', $wrapper).removeClass('selected');
$(this).addClass('selected');
if ($select[0].selectedIndex != $(this).attr('index') && $select[0].onchange) {
$select[0].selectedIndex = $(this).attr('index');
$select[0].onchange();
}
$select[0].selectedIndex = $(this).attr('index');
$('span:eq(0)', $wrapper).html($(this).html());
$ul.hide();
return false;
});
$('a:eq(' + this.selectedIndex + ')', $ul).click();
$('span:first', $wrapper).click(function () {
$("a.jqTransformSelectOpen", $wrapper).trigger('click');
});
oLabel && oLabel.click(function () {
$("a.jqTransformSelectOpen", $wrapper).trigger('click');
});
this.oLabel = oLabel;
var oLinkOpen = $('a.jqTransformSelectOpen', $wrapper).click(function () {
if ($ul.css('display') == 'none') {
jqTransformHideSelect();
}
if ($select.attr('disabled')) {
return false;
}
$ul.slideToggle('fast', function () {
var offSet = ($('a.selected', $ul).offset().top - $ul.offset().top);
$ul.animate({
scrollTop: offSet
});
});
return false;
});
var iSelectWidth = $select.outerWidth() + 15;
var oSpan = $('span:first', $wrapper);
var newWidth = (iSelectWidth > oSpan.innerWidth()) ? iSelectWidth + oLinkOpen.outerWidth() : $wrapper.width();
$wrapper.css('width', newWidth);
$ul.css('width', newWidth - 7);
oSpan.css({
width: iSelectWidth
});
$ul.css({
display: 'block',
visibility: 'hidden'
});
var iSelectHeight = ($('li', $ul).length) * ($('li:first', $ul).height());
(iSelectHeight < $ul.height()) && $ul.css({
height: iSelectHeight,
'overflow': 'hidden'
});
$ul.css({
display: 'none',
visibility: 'visible'
});
});
};
我想要做的是在隐藏原始选择框之前获取它们的 ID,并将它们添加到新创建的 $wrapper 中 - 我将如何做到这一点?
谢谢, 克里斯
I have a function which creates HTML elements out of a select box in order to allow styling. Here's the code:
$.fn.jqTransSelect = function () {
return this.each(function (index) {
var $select = $(this);
if ($select.hasClass('jqTransformHidden')) {
return;
}
if ($select.attr('multiple')) {
return;
}
var oLabel = jqTransformGetLabel($select);
var $wrapper = $select.addClass('jqTransformHidden').wrap('<div class="jqTransformSelectWrapper"></div>').parent().css({
zIndex: 10 - index
});
$wrapper.prepend('<div><span></span><a href="#" class="jqTransformSelectOpen"></a></div><ul></ul>');
var $ul = $('ul', $wrapper).css('width', $select.width() + 10).hide();
$('option', this).each(function (i) {
var oLi = $('<li><a href="#" index="' + i + '">' + $(this).html() + '</a></li>');
$ul.append(oLi);
});
$ul.find('a').click(function () {
$('a.selected', $wrapper).removeClass('selected');
$(this).addClass('selected');
if ($select[0].selectedIndex != $(this).attr('index') && $select[0].onchange) {
$select[0].selectedIndex = $(this).attr('index');
$select[0].onchange();
}
$select[0].selectedIndex = $(this).attr('index');
$('span:eq(0)', $wrapper).html($(this).html());
$ul.hide();
return false;
});
$('a:eq(' + this.selectedIndex + ')', $ul).click();
$('span:first', $wrapper).click(function () {
$("a.jqTransformSelectOpen", $wrapper).trigger('click');
});
oLabel && oLabel.click(function () {
$("a.jqTransformSelectOpen", $wrapper).trigger('click');
});
this.oLabel = oLabel;
var oLinkOpen = $('a.jqTransformSelectOpen', $wrapper).click(function () {
if ($ul.css('display') == 'none') {
jqTransformHideSelect();
}
if ($select.attr('disabled')) {
return false;
}
$ul.slideToggle('fast', function () {
var offSet = ($('a.selected', $ul).offset().top - $ul.offset().top);
$ul.animate({
scrollTop: offSet
});
});
return false;
});
var iSelectWidth = $select.outerWidth() + 15;
var oSpan = $('span:first', $wrapper);
var newWidth = (iSelectWidth > oSpan.innerWidth()) ? iSelectWidth + oLinkOpen.outerWidth() : $wrapper.width();
$wrapper.css('width', newWidth);
$ul.css('width', newWidth - 7);
oSpan.css({
width: iSelectWidth
});
$ul.css({
display: 'block',
visibility: 'hidden'
});
var iSelectHeight = ($('li', $ul).length) * ($('li:first', $ul).height());
(iSelectHeight < $ul.height()) && $ul.css({
height: iSelectHeight,
'overflow': 'hidden'
});
$ul.css({
display: 'none',
visibility: 'visible'
});
});
};
What I'm wanting to do is take the ID's of the original select boxes before they are hidden, and add them ass classes to the newly created $wrapper - how would I do this?
Thanks,
Chris
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
之后:
放置:
这是一个示例。
After:
Put:
Here is an example.