2011-03-11 98 views
14

我在jQuery UI的主题页面上看到了这些,并认为它们很整洁。用jQuery UI显示消息

我不知道如何显示这些风格都以静态方式使用JavaScript消息。

在此先感谢。

回答

14

使用Chrome开发人员工具,我检查了错误消息的HTML。您可以为另一个做同样的事情,或者您可以查看jQuery UI CSS Framework

HTML

​​

CSS

body { 
    font-family: Verdana,Arial,sans-serif; 
    font-size: 10px; 
} 

p { 
    margin: 1em 0; 
} 

strong { 
    font-weight: 900; 
} 

可以使用addClass方法添加这些类以编程方式使用JS。另请参阅可用于显示/隐藏这些消息的showhide方法。

<button id="show">Show</button> 
<button id="hide">Hide</button> 

$("#show").click(function() { 
    $(".ui-widget").show(); 
}); 

$("#hide").click(function() { 
    $(".ui-widget").hide(); 
}); 

请参阅fiddle

0

复制jQuery UI生成的类和HTML结构,并确保包含jQuery UI主题。

1

只需使用萤火虫检查该元素的HTML。看起来他们正在使用<div style="margin-top: 20px; padding: 0pt 0.7em;" class="ui-state-highlight ui-corner-all"> <p><span style="float: left; margin-right: 0.3em;" class="ui-icon ui-icon-info"></span> <strong>Hey!</strong> Sample ui-state-highlight style.</p> </div>

1

我已经修改了一个简短的jQuery函数来将给定的一组div(包含文本)转换为错误/高亮元素。

你可以在this jsFiddle上看到它的行动。

这里是JavaScript:

//function to create error and alert dialogs 
function errorHighlight(e, type, icon) { 
    if (!icon) { 
     if (type === 'highlight') { 
      icon = 'ui-icon-info'; 
     } else { 
      icon = 'ui-icon-alert'; 
     } 
    } 
    return e.each(function() { 
     $(this).addClass('ui-widget'); 
     var alertHtml = '<div class="ui-state-' + type + ' ui-corner-all" style="padding:0 .7em;">'; 
     alertHtml += '<p>'; 
     alertHtml += '<span class="ui-icon ' + icon + '" style="float:left;margin-right: .3em;"></span>'; 
     alertHtml += $(this).text(); 
     alertHtml += '</p>'; 
     alertHtml += '</div>'; 

     $(this).html(alertHtml); 
    }); 
} 

//error dialog 
(function($) { 
    $.fn.error = function() { 
     errorHighlight(this, 'error'); 
    }; 
})(jQuery); 

//highlight (alert) dialog 
(function($) { 
    $.fn.highlight = function() { 
     errorHighlight(this, 'highlight'); 
    }; 
})(jQuery);