获取点击元素jQuery的HTML内容 [英] get the HTML content of clicked element jQuery

查看:261
本文介绍了获取点击元素jQuery的HTML内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下HTML

 < label class =editableid =user_info_username>海世界。 ..< /标签> 

现在点击功能我需要点击元素的内容。



我试过了
$ b $ $ p $ $ $ $ $ $。editable)。live(click,function() {
alert($(this).html())//返回海世界...
});

但我需要HTML内容

所以< label class =editableid =user_info_username>海洋世界...< / label>

< div> 中,然后询问HTML的内容 - 例如:

  var html = $('< div />')。append($(this).clone ())的HTML(); 

http://jsfiddle.net/f88kj/

我之前也写过一个插件,它在 https://stackoverflow.com/a/6509421/6782

 < code(function($){
$ .fn.outerhtml = function(){
return $('< div />')。append(this.clone()) .html();
};
})(jQuery);


I have the following HTML

<label class="editable" id="user_info_username">Hai world...</label>

now on click function i need the content of the clicked element .

i tried

$(".editable").live("click",function(){
alert($(this).html())  //returns Hai world...
});

But i need the HTML content

so <label class="editable" id="user_info_username">Hai world...</label>

解决方案

Clone the clicked element, wrap it in a <div>, and then ask for the HTML of that - something like:

var html = $('<div/>').append($(this).clone()).html();

Working demo at http://jsfiddle.net/f88kj/

I also previously wrote a plugin that does this at https://stackoverflow.com/a/6509421/6782

(function($) {
    $.fn.outerhtml = function() {
        return $('<div/>').append(this.clone()).html();
    };
})(jQuery);

这篇关于获取点击元素jQuery的HTML内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆