2013-10-16 239 views
0

所以 - jQuery UI库。有一个对话框组件。这个组件还有一个“标题”选项。我如何重写jQuery的默认功能,以便每当我为对话设置一个新标题时 - 它在它前面添加“xxx”?Jquery覆盖默认功能

+1

它只是一个'具有title属性div'标签,也许我不明白的问题? – SpaceBison

+1

您可以提供代码示例,包括html和javascript代码吗? – ovunccetin

+0

不能理解Eriks亲爱的问题! – Neel

回答

1

我会写一个辅助方法来设置标题和调用来代替:

function setTitleWithPrefix(selector, title) { 
    $(selector).dialog('option', 'title', 'xxx' + title); 
} 

你甚至可以将其创建为一个jQuery插件:

$.fn.dialogTitleWithPrefix = function(title) { 
    this.each(function() { 
     $(this).dialog('option', 'title', 'xxx' + title); 
    }); 
}; 

这可以被称为:

$('.myDialog').dialogTitleWithPrefix('New Title'); 

两种方法的工作示例 - http://jsfiddle.net/kReA5/1/

或者,如果要扩展对话框小部件本身,请查看此问题的答案(How to extend an existing jQuery UI widget?)。

0

可以重写jQuery.dialog方法

var oldDialog = jQuery.fn.dialog; 
jQuery.fn.dialog = function(title, options, otherStuff) { 
    title = 'xxx' + title; 
    return this.oldDialog(title, options, otherStuff); 
}); 

不过,我不知道你用这种方法使用的其他选项。如果你提供一些代码给你的问题,我会更新我的答案。

0

嗯,这会使你的对话框:

<div id="dialog" title="Your title"> 
    <p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p> 
</div> 

它将与所选标题使其但是如果你现在也想在前面加上XXX的标题,您需要更改标题该div后属性值。

要做到这一点使用下面的脚本:

<script> 
    var title = $("#dialog").getAttribute("title"); // This will return "YOUR TITLE" 

    // Now we change the title to xxx YOUR TITLE 
    $("#dialog").attr("title", "XXX" + title); 
</script>