2

我将jQuery UI 主题与我的一个 Web 应用程序一起使用。

如果我想应用按钮样式,我添加以下 jquery 代码:

$(".button").button();

.button我希望使用 jquery 主题设置样式的按钮上的类在哪里。

在此处输入图像描述


我怎样才能做类似的事情来将主题样式应用到我想将样式设置为“突出显示”的元素?

在此处输入图像描述

我试过.highlight();了,但这没有奏效。

注意:我知道我可以手动将 CSS 类添加到我的元素中,但我希望将其与 jQuery 一起应用,因为这样可以节省我添加span显示图标的元素的时间。

因此,我希望能够拥有以下 HTML 代码:

<div class="highlight">
   <strong>Warning!</strong> Lorem Ipsum
</div>

然后使用 jQuery 将其转换为:

<div class="highlight ui-state-highlight ui-corner-all" style="margin-bottom:20px">
   <p><span class="ui-icon ui-icon-info" style="float: left; margin-right: .3em;"></span>
   <strong>Warning!</strong> Lorem Ipsum</p>
</div>

或者,我是否误解了主题滚轮页面上“突出显示”示例的用途?我假设这是一个警告框,考虑到它旁边是一个错误示例。

4

3 回答 3

1

http://jsfiddle.net/mTu2R/3/

$.fn.highlight = function() {
    return this.each(function() {

        var elem = $(this), p = $("<p>", {
            html: '<span class="ui-icon ui-icon-info" style="float: left; margin-right: .3em;"></span>' + 
                  elem.html()
        });

        elem.empty().addClass("ui-state-highlight ui-corner-all").append(p);

    });
};

$(".highlight").highlight();
于 2012-06-25T16:40:16.560 回答
0

我觉得我可能错过了这里的重点,但你说过你想“用 jQuery”做这个,所以:

$(".highlight").each(function() {
    var elm, p;

    elm = $(this);
    // Create the new paragraph and span
    p = $('<p><span class="ui-icon ui-icon-info" style="float: left; margin-right: .3em;"></span></p>');
    // Add the classes to the element
    elm.addClass("ui-state-highlight ui-corner-all");
    // Move the element's children into the paragraph
    p.append(elm.contents());
    // Append the paragraph
    elm.append(p);
});

实例| 资源

于 2012-06-25T16:44:15.973 回答
0

插件保留现有的 ID 和类

(function($){
$.fn.highlight=function(){
     return this.each(function(){                              
            var text=$(this).text();
            var id=this.id;
            var thisClass=$(this).attr('class');
            var $el=$('<div class="highlight ui-state-highlight ui-corner-all" style="margin-bottom:20px">\
                   <p><span class="ui-icon ui-icon-info" style="float: left; margin-right: .3em;"></span>\
                   <strong>'+text+'</strong> Lorem Ipsum</p>\
                </div>').attr(id, id).addClass( thisClass);

             $(this).replaceWith($el);
        });

};
})(jQuery);

$(anyElement).highlight();
于 2012-06-25T16:27:20.300 回答