Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 从元素的title属性获取文本,同时附加到元素_Jquery_Dom_Dom Manipulation - Fatal编程技术网

Jquery 从元素的title属性获取文本,同时附加到元素

Jquery 从元素的title属性获取文本,同时附加到元素,jquery,dom,dom-manipulation,Jquery,Dom,Dom Manipulation,我有一个函数,它可以在所有元素之后附加div(帮助图标)和.help类 jQuery().ready(function() { jQuery('.help').append('<div class="mark"></div>'); }); jQuery().ready(函数()){ jQuery('.help').append(''); }); 要从.help元素的title属性获取信息,我需要向该函数添加什么 我是脚本新手。尝试使用以下功能: var ti

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

jQuery().ready(function() {
     jQuery('.help').append('<div class="mark"></div>');
});
jQuery().ready(函数()){
jQuery('.help').append('');
});
要从
.help
元素的title属性获取信息,我需要向该函数添加什么

我是脚本新手。

尝试使用以下功能:

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

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

我想这就是你想要的:

jQuery().ready(function() {
jQuery('.help').each( function(index, elem){
    $(this).append('<div class="mark" title="' + $(this).attr("title") + '"></div>')
});
jQuery().ready(函数()){
jQuery('.help')。每个(函数(索引,元素){
$(this.append(“”)
});
}))

我想你在寻找这样的东西:

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);
    });
});
jQuery().ready(函数()){
//对于每个帮助元素。。。。
jQuery('.help')。每个(函数(){
//创建空标记,从“帮助”添加标题,将标记附加到“帮助”
$('').attr('title'),$(this.attr('title')).appendTo(this);
});
});
这将向每个帮助元素添加一个
div.mark
,并将标记的标题设置为父帮助的标题。

利用这一事实,还可以接受一个函数,该函数将创建要附加的HTML字符串:

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

您是否需要使用“help”类获取或设置每个元素的“title”属性信息?请查看jQuery API文档()中的
每个
方法。我不太理解您的问题。你能告诉我更多关于你的问题吗?@ctekse-我们的想法是一样的:)太好了,这就是我要找的:]是的@johnhunter,我很高兴发布我(有史以来第一次)的答案,只是看到其他人在同一分钟内也在做同样的事情:)太好了,这就是我要找的
$('.help').append(function () {
    return '<div class="mark" title="' + $(this).attr('title') + '"></div>';
});