jQuery 如何使用jquery更改div标题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2696682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:05:34  来源:igfitidea点击:

How to change div title using jquery

jquery

提问by learning

I have the following code:

我有以下代码:

<div id="DivPassword" title="test" > 

I want to change the div title and I have the following code:

我想更改 div 标题,我有以下代码:

 function ChangeAttribute() {
         $("#DivPassword")
            .attr('title', 'Photo by Kelly Clark');
         $('#DivPassword').dialog('open');
         return false;
     }

When the dialog is opened, the title is still test! if I don't assign any title to the div, the dialog doesn't show any title. How can I correct that?

当对话框打开时,标题仍然是测试!如果我没有为 div 分配任何标题,则对话框不会显示任何标题。我该如何纠正?



 function ChangeAttribute() {
         $("#DivPassword")
            .attr('title', 'Photo by Kelly Clark')
            .dialog('open');

         alert($("#DivPassword").attr('title'));
     }

$('#DivPassword').dialog({
             autoOpen: false,
             width: 800,
             buttons: {
                 "Cancel": function() {
                     $(this).dialog("close");
                 },
                 "Accept": function() {
                 alert($(this).attr('title'));
                     $(this).dialog("close");
                 }
             }
         });


 <script type="text/javascript">
     var Dtitle;
     $(function() {
        $('#DivPassword').dialog({

             autoOpen: false,
             width: 800,
             title : Dtitle,
             buttons: {
                 "Cancel": function() {
                     $(this).dialog("close");
                 },
                 "Accept": function() {
                     $(this).dialog("close");
                 }
             }
         });
     });

     function ChangeAttribute(name) {
         $("#DivPassword")
            .attr('title', name)
            .dialog('open');
         Dtitle = $("#DivPassword").attr('title');
         alert(Dtitle);
     }


</script>

采纳答案by learning

Thanks for all the answers.

感谢所有的答案。

The $('#DivPassword').dialog({had to be after .dialog('open');

$('#DivPassword').dialog({后不得不.dialog('open');

The simplest way was to do as follows:

最简单的方法是这样做:

 $("#DivPassword")
            .dialog('open');
         $('#DivPassword').dialog({
             autoOpen: false,
             title: $('#DivPassword').attr('title') + name,
             width: 400,
             buttons: {
                 "Cancel": function() {
                     $(this).dialog("close");
                 },
                 "Accept": function() {
                     $(this).dialog("close");
                 }
             }
         });

回答by Christian Studer

You can change the title of a dialog directly with:

您可以使用以下命令直接更改对话框的标题:

$('#DivPassword').dialog('option', 'title', 'Photo by Kelly Clark');

This will work in your case. In fact, your code to change the title attribute is correct. I guess that the dialog plugin creates the dialog when .dialog is called first. The open method just displays the dialog then, without re-creating it from the div.

这将适用于您的情况。实际上,您更改 title 属性的代码是正确的。我猜对话框插件会在首先调用 .dialog 时创建对话框。然后 open 方法只显示对话框,而无需从 div 重新创建它。

回答by Jhollman

You can change ANY attribute from any DOM element with jQuery as easy as:

您可以使用 jQuery 更改任何 DOM 元素的任何属性,就像:

$("#divMessage").attr('title', 'Type here the new Title text');

Cheers!

干杯!

回答by Ustes Greenridge

$("#div1").dialog({ autoOpen: false, modal: true, height: 420, width: 750, resizable: false });
// more code
$("#div1").dialog("option", "title", "joopla, here is a new title");

did the trick for me..

为我做的伎俩..

回答by AaronThomson

Have you tried any of the suggestions given here: How to change an element's title attribute using jQuery?

您是否尝试过此处给出的任何建议: 如何使用 jQuery 更改元素的标题属性

It looks like you are doing what the accepted answer suggests. If this doesn't work, perhaps the others will.

看起来您正在按照公认的答案进行操作。如果这不起作用,也许其他人会。

回答by Michel

i did this:

我这样做了:

$("#div1").dialog({ autoOpen: false, modal: true, height: 420, width: 750, resizable: false });
//more code
$("#div1").dialog("option", "title", "joopla, here is a new title");

This works for me

这对我有用

回答by Reigel

Specifies the title of the dialog. The title can also be specified by the title attribute on the dialog source element.

指定对话框的标题。标题也可以由对话框源元素上的 title 属性指定。

Code examples

代码示例

Initialize a dialog with the title option specified.

使用指定的标题选项初始化对话框。

$( ".selector" ).dialog( { title: 'Dialog Title' } );

Get or set the title option, after init.

在 init 之后获取或设置标题选项。

//getter
var title = $( ".selector" ).dialog( "option", "title" );
//setter
$( ".selector" ).dialog( "option", "title", 'Dialog Title' );

source.

来源

to answer your the dialog doesnt show any title.

回答您的the dialog doesnt show any title.

回答by nickf

It sounds like your plugin is reading the title before it gets to that ChangeAttribute function. The code you've posted will certainly change the title attribute, so there's something else going on behind the scenes.

听起来您的插件在到达 ChangeAttribute 函数之前正在阅读标题。您发布的代码肯定会更改 title 属性,因此幕后还有其他事情发生。

Also, while I'm here, don't forget to use chaining. Most functions in jQuery return the same object again:

另外,当我在这里时,不要忘记使用链接。jQuery 中的大多数函数再次返回相同的对象:

$("#DivPassword").attr('title', 'Photo by Kelly Clark').dialog('open');

// or like this, perhaps easier to read:

$("#DivPassword")
    .attr('title', 'Photo by Kelly Clark')
    .dialog('open')
;

回答by rahul

Try setting the title attribute after the dialog is open. Probably your dialog.open() will be resetting the title attribute.

尝试在对话框打开后设置 title 属性。可能您的 dialog.open() 将重置标题属性。

I think you can supply a callback function for the open event.

我认为您可以为 open 事件提供回调函数。

See open

查看打开