【问题标题】:jQuery - how to set title attribute in a particular formatjQuery - 如何以特定格式设置标题属性
【发布时间】:2013-08-29 10:32:03
【问题描述】:

我将这个 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');

但它没有像我尝试的那样工作,按照答案包含的链接,也许标题属性不适用于&lt;div&gt; 标签,而是&lt;a&gt; 标签。

【问题讨论】:

  • 你确定 'title':'text here' 没用吗?我 99% 确定这是正确的。
  • 它工作正常:codepen.io/Fresheyeball/pen/Foayb 你的问题不是这个语法。它是别的东西。
  • 谢谢,代码显然适用于所有 3 个变体的链接:title:'text here'、'title':'text here',并在末尾附加 .attr("title", “在这打字” )。也许问题在于将代码分配给变量。 ivowiblo 提出了简单的解决方案,但仍然令人困惑。

标签: jquery attributes


【解决方案1】:

试试这个:

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

names.attr("title", "text here");

【讨论】:

  • 感谢 ivowiblo,此代码按预期工作。但是当我将它链接到语句的末尾时,我不明白为什么它不起作用。
猜你喜欢
  • 2013-08-19
  • 1970-01-01
  • 2014-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-12
  • 1970-01-01
  • 2011-09-15
相关资源
最近更新 更多