简体   繁体   English

如何打开jQuery对话框并将其标题自动设置为HTML标记中的属性?

[英]How do I open a jQuery dialog and auto-set its title to the attribute in the HTML tag?

I have a div that I'm setting a title attribute on: 我有一个div,我要在其上设置title属性:

<div id="test" title="Test Dialog">Some test</div>

Then I open a dialog with it: 然后打开一个对话框:

$('#test').dialog({
    title: $(this).attr('title')
});

The first time this dialog opens, the title is set successfully. 第一次打开此对话框时,标题设置成功。 However, if I close the dialog and re-open it, the title will be blank. 但是,如果我关闭对话框并重新打开它,标题将为空白。 This is because, the first time jQuery opens the dialog, it copies the element and removes the title tag. 这是因为jQuery第一次打开对话框时,它会复制元素并删除title标签。 Is there a way to get the behavior I want, where it reads the title from the title attribute every time? 有没有一种方法可以得到我想要的行为,它每次都会从title属性读取标题?

I'd do this in the open event, like so: 我会在open中这样做,就像这样:

$("#test").dialog({
   open: function(event, ui) { 
      $(this).dialog( "option", "title", $(this).attr('title') );
   }
});

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

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