[英]Jquery to get the contents of a div
How can I get the contents of a <div>
when a link is clicked 单击链接时如何获取
<div>
的内容
eg : 例如:
there are two div
tags 有两个
div
标签
<div id="1" > This Is First Div </div>
<div id="2" > This Is Second Div </div>
<a href="#" id="click" >Click Here</a>
When the link is clicked, I need to get the content as: 单击链接后,我需要获取以下内容:
This Is Fist Div
这是拳头
This Is Second Div这是第二师
If I well understood 如果我很好理解
$('a').on('click', function(ev) {
ev.preventDefault();
$($(this).prevAll('div').get().reverse()).each(function() {
alert ($(this).text())
})
});
As a sidenote, the id
attributes you choose are not valid (they cannot start with a digit) 附带说明,您选择的
id
属性无效(它们不能以数字开头)
Example fiddle : http://jsfiddle.net/533qX/ 小提琴示例: http : //jsfiddle.net/533qX/
edit. 编辑。 With one single alert
一个警报
$('a').on('click', function(ev) {
var text = "";
ev.preventDefault();
$($(this).prevAll('div').get().reverse()).each(function() {
text += $(this).text() + "\n";
})
alert(text);
});
function alertDiv(divId){
$('#click').on('click', function(e){
alert ($(divId).text());
e.preventDefault();
});
}
alertDiv('#one');
alertDiv('#two');
http://jsbin.com/ebowim/2/edit http://jsbin.com/ebowim/2/edit
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.