附加到元素时从元素的标题属性获取文本

发布于 2024-10-21 13:47:39 字数 264 浏览 8 评论 0原文

我有一个函数,可以在带有 .help 类的所有元素之后附加 div (帮助图标)。

jQuery().ready(function() {
     jQuery('.help').append('<div class="mark"></div>');
});

我需要向该函数添加什么才能从 .help 元素的 title 属性获取信息?

我是脚本新手。

I have function which append div (help icon) after all element with .help class.

jQuery().ready(function() {
     jQuery('.help').append('<div class="mark"></div>');
});

What do I need to add to that function to get information from title attribute of .help element?

I'm new in scripting.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

攒一口袋星星 2024-10-28 13:47:39

利用 append() 也可以接受一个函数来创建要附加的 HTML 字符串:

$('.help').append(function () {
    return '<div class="mark" title="' + $(this).attr('title') + '"></div>';
});

Utilizing the fact that append() can also accept a function that will create the HTML string to append:

$('.help').append(function () {
    return '<div class="mark" title="' + $(this).attr('title') + '"></div>';
});
寄意 2024-10-28 13:47:39

我想这就是你想要的:

jQuery().ready(function() {
jQuery('.help').each( function(index, elem){
    $(this).append('<div class="mark" title="' + $(this).attr("title") + '"></div>')
});

});

I think this is what you want:

jQuery().ready(function() {
jQuery('.help').each( function(index, elem){
    $(this).append('<div class="mark" title="' + $(this).attr("title") + '"></div>')
});

});

椵侞 2024-10-28 13:47:39

我认为您正在寻找类似的内容:

jQuery().ready(function() {

    // with each help element....
    jQuery('.help').each(function () {

        // create the empty mark, add the title from help, append mark to help
        $('<div class="mark"></div>').attr('title', $(this).attr('title')).appendTo(this);
    });
});

这将为每个帮助元素添加一个 div.mark 并将该标记的标题设置为父帮助的标题。

I think you are looking for something like this:

jQuery().ready(function() {

    // with each help element....
    jQuery('.help').each(function () {

        // create the empty mark, add the title from help, append mark to help
        $('<div class="mark"></div>').attr('title', $(this).attr('title')).appendTo(this);
    });
});

This will add a div.mark to each help element and set the title of the mark to be the title of the parent help.

梦屿孤独相伴 2024-10-28 13:47:39
var title = $('.help').attr("title");
var title = $('.help').attr("title");
怪我太投入 2024-10-28 13:47:39
var title = jQuery('.help').attr('title');

jQuery('.mark').attr('title',title);
var title = jQuery('.help').attr('title');

jQuery('.mark').attr('title',title);
小清晰的声音 2024-10-28 13:47:39

尝试使用 attr 函数:

jQuery().ready(function() { 
    console.log( jQuery('.help').attr( 'title' ) );
});

Try to use the attr function:

jQuery().ready(function() { 
    console.log( jQuery('.help').attr( 'title' ) );
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文