0

我将这个 jQuery 代码作为循环的一部分来生成 div 并为每个 div 设置属性。

var names = $('<div/>', {
         id: load1array[j],
    'class': 'bold',
     html: load1array[j]
        });

我想为每个 div 添加一个标题属性,但我忘记了语法。我已经尝试了下面的代码,但它们似乎不是正确的语法。

var names = $('<div/>', {
         id: load1array[j],
    'class': 'bold',

//attr:('title','text here'),        
//title:'text here' ,    
//'title':'text here' ,  

        html: load1array[j]
        })//.attr( "title", "text here" );

TIA

编辑:

我也在这里看到了答案Creating a div element in jQuery,这表明包含title属性的代码

jQuery('<div/>', {
    id: 'foo',
    href: 'http://google.com',
    title: 'Become a Googler',
    rel: 'external',
    text: 'Go to Google!'
}).appendTo('#mySelector');

但它没有像我尝试过的那样工作,在答案包含的链接之后,也许 title 属性不适用于<div>标签,而是<a>标签。

4

1 回答 1

1

尝试这个:

var names = $('<div/>', {
     id: load1array[j],
'class': 'bold',
 html: load1array[j]
    });

names.attr("title", "text here");
于 2013-08-29T01:58:52.180 回答