Javascript 获取div not span的文本
我有这个演示,并试图获得div的文本 想换一条新线路吗 如果div有id foo或class fooJavascript 获取div not span的文本,javascript,jquery,Javascript,Jquery,我有这个演示,并试图获得div的文本 想换一条新线路吗 如果div有id foo或class foo $("#foo") //can not use div as will target all the divs .clone() //clone the element .children() //select all the children .remove() //remove all the children .end() //again go
$("#foo") //can not use div as will target all the divs
.clone() //clone the element
.children() //select all the children
.remove() //remove all the children
.end() //again go back to selected element
.text();
或者
$("#foo").contents()
.filter(function() {
return this.nodeType === 3;
}).text()
像这样试试
如果div有id foo或class foo
$("#foo") //can not use div as will target all the divs
.clone() //clone the element
.children() //select all the children
.remove() //remove all the children
.end() //again go back to selected element
.text();
或者
$("#foo").contents()
.filter(function() {
return this.nodeType === 3;
}).text()
就这样实现:
var anchors = $('<div/>').append(string).text();//find() is removed as it will find the text not html span.
就这样实现:
var anchors = $('<div/>').append(string).text();//find() is removed as it will find the text not html span.
JS:
JS:
试试这个:
var string = "<span>this is span</span>new line";
var jdiv = $('<div/>');
var jspan = jdiv.append(string).find('span');
//get whatever you want
jspan.text();//
jdiv.text();//
试试这个:
var string = "<span>this is span</span>new line";
var jdiv = $('<div/>');
var jspan = jdiv.append(string).find('span');
//get whatever you want
jspan.text();//
jdiv.text();//
这将使用右选择器查找div中的所有文本节点
console.log($('div').contents()
.filter(function() {
return this.nodeType === 3;
}).text())
这将使用右选择器查找div中的所有文本节点
console.log($('div').contents()
.filter(function() {
return this.nodeType === 3;
}).text())