[英]Get clicked element html with jQuery?
I'm listening for clicks of a number of element's.我在听一些元素的点击。 I want to show an alert when an user clicks on one, the alert's content should be the clicked element's innerHtml.我想在用户点击一个警报时显示一个警报,警报的内容应该是被点击元素的innerHtml。
$(".tags-container > p").click(function (event) {
alert('inner html');
});
I could do it by adding the code to the html, but I prefer to use a jquery listener.我可以通过将代码添加到 html 来实现,但我更喜欢使用 jquery 侦听器。
How can I show the element's innerHTML on the alert?如何在警报中显示元素的 innerHTML?
Get the HTML out of the <p>
tag using jQuery's html() :使用 jQuery 的html()从<p>
标签中取出HTML :
$(".tags-container > p").click(function(event) {
alert($(this).html());
});
Get the HTML out of the <p>
tag using JavaScript's innerHTML :使用 JavaScript 的innerHTML从<p>
标签中取出 HTML:
$(".tags-container > p").click(function(event) {
alert(this.innerHTML);
});
Get just the text out of the <p>
tag using jQuery's text() , which will strip out any HTML and return the combined text.使用 jQuery 的text()仅从<p>
标记中获取文本,这将去除任何 HTML 并返回组合文本。
$(".tags-container > p").click(function(event) {
alert($(this).text());
});
$(".tags-container > p").click(function () {
alert($(this).html());
});
use jQuery's html()
function使用 jQuery 的html()
函数
$(".tags-container > p").click(function (event) {
alert($(this).html());
});
a working example: http://jsfiddle.net/ukxvp/一个工作示例: http : //jsfiddle.net/ukxvp/
Like they said, just use $.html().就像他们说的,只需使用 $.html()。 Personally, I'd just use innerHTML because $.html() filters out br elems and such;就个人而言,我只使用 innerHTML 因为 $.html() 过滤掉 br elems 等; I'd just turn them into newlines.我只是把它们变成换行符。
$(".tags-container > p").click(function () {
alert(this.innerHTML.replace(/<br>/, '\n'));
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.