[英]How to get content of a non-specific DIV-Element using jQuery
I've a problem and I hope that it's possible to explain. 我有一个问题,希望可以解释。
I have a lot of short texts which come from a mysql-DB. 我有很多来自mysql-DB的简短文本。
while ($foo = mysql_fetch_object($query)) {
$text = $foo -> column_text_db;
echo '<div class="atextdiv">' . $text . '</div>';
}
now i want to add an onclik-Event-Handler to the "atextdiv"-Container(s) who takes the content and put it on an input-field: 现在,我想向“ atextdiv”容器添加onclik-Event-Handler,以获取内容并将其放置在输入字段中:
<form>
<input type="text" id="clickcontent" />
</form>
My problem: Each page has a different number of records in database. 我的问题:每个页面在数据库中都有不同数量的记录。 The final HTML-Code (after parsing the PHP-Code) could be:
最终的HTML代码(在解析PHP代码之后)可能是:
<div id="wrapper">
<div class="atextdiv">Papa was a rolling stone</div>
<div class="atextdiv">Oh my god! They killed Kenny</div>
<div class="atextdiv">Foo Bar</div>
<!-- more content -->
</div>
<form>
<input type="text" id="clickcontent" />
</form>
So, how can I get the content of each "atextdiv"-Container after click on it? 那么,单击后如何获取每个“ atextdiv”容器的内容?
Just like: 就像:
$(".atextdiv").click(function() {
console.log($(this).text());
});
Use a combination of this
and the .text
method inside the click handler. this
与点击处理程序中的.text
方法结合使用。
Just grab the innerHTML of the item that the click event is occurring on: 只需获取发生单击事件的项目的innerHTML即可:
$('.atextdiv').click(function(e){
alert(e.currentTarget.innerHTML);
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.