I have an xml file like:
我有一个xml文件,如:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<legends>
<legend>
<number>1</number>
<legendString><ul><li>1.across:a system of rules of conduct or method of practice</li><li>1.down:a disagreement or argument about something important</li><li>2.down:any broad thin surface</li><li>3.across:the passage of pedestrians or vehicles </li></ul></legendString>
</legend>
......
<legends>
How can I take whole text including tags using jQuery. The string
如何使用jQuery获取包括标签在内的全文。字符串
<ul><li>1.across:a system of rules of conduct or method of practice</li><li>1.down:a disagreement or argument about something important</li><li>2.down:any broad thin surface</li><li>3.across:the passage of pedestrians or vehicles </li></ul>
should be passed if I make a function call. If I call:
如果我进行函数调用,应该通过。如果我打电话:
var legendStr = $(this).find("legendString").text();
tags will not be present in legendStr.
legendStr中不会出现标记。
How can I proceed.
我该怎么办?
This is the jQuery Script:
这是jQuery脚本:
var randomnumber=Math.floor(Math.random()*233);
$.get("legends.xml",{},function(xml){
$(xml).find("legend").each(function(){
if(randomnumber == $(this).find("number").text())
{
var c = "legendString";
var legendStr = $(this).find(c).html();
$("#sidebar > ul").html(legendStr);
}
});
},"xml");
3 个解决方案
#1
1
You want $(this).find("legendString").html()
你想要$(this).find(“legendString”)。html()
I took a closer look at jQuery's support for XML. .html()
doesn't support XML document. So, you'll need to either use the traversing methods and/or XML DOM.
我仔细研究了jQuery对XML的支持。 .html()不支持XML文档。因此,您需要使用遍历方法和/或XML DOM。
So, For example, you could do a depth-first traversal till you find a text node (i.e. nodeType == 3), and you can use nodeName
(e.g. <ul>
) and .text() to get the tag name and the text.
因此,例如,您可以执行深度优先遍历,直到找到文本节点(即nodeType == 3),并且您可以使用nodeName(例如
-
)和.text()来获取标记名称和文本。
#2
0
Try using the .html()
method to preserve tags:
尝试使用.html()方法来保留标记:
var legendStr = $(this).find('legendString').html();
Also note that .find('legendString')
will look for a tag called legendString
which probably is not what you are looking for. You probably need the :contains()
selector.
另请注意,.find('legendString')将查找名为legendString的标记,这可能不是您要查找的标记。您可能需要:contains()选择器。
#3
0
I don't think the function you need is in jQuery out-of-the-box. But you can write your own. Here is a starting point for you:
我不认为你需要的功能是jQuery开箱即用。但你可以写自己的。以下是您的出发点:
jQuery.extend(jQuery.fn, {
outerHtml: function(tagName) {
var matches = this.find(tagName);
var result = '';
for (var i=0; i<matches.length; i++) {
var el = matches[i];
result += '<' + tagName;
for (var i=0; i<el.attributes.length; i++) {
result += ' ';
result += el.attributes[i].name;
result += "='";
result += el.attributes[i].value;
result += "'";
}
if (el.innerHTML == null) { result += '/>'; }
else {
result += '>';
result += el.innerHTML;
result += '</'
result += tagName;
result += '>';
}
return result;
}
}});
}});
thus, you'll be able to do
因此,你将能够做到
var legendString = $(this).outerHtml('legendString');
#1
1
You want $(this).find("legendString").html()
你想要$(this).find(“legendString”)。html()
I took a closer look at jQuery's support for XML. .html()
doesn't support XML document. So, you'll need to either use the traversing methods and/or XML DOM.
我仔细研究了jQuery对XML的支持。 .html()不支持XML文档。因此,您需要使用遍历方法和/或XML DOM。
So, For example, you could do a depth-first traversal till you find a text node (i.e. nodeType == 3), and you can use nodeName
(e.g. <ul>
) and .text() to get the tag name and the text.
因此,例如,您可以执行深度优先遍历,直到找到文本节点(即nodeType == 3),并且您可以使用nodeName(例如
-
)和.text()来获取标记名称和文本。
#2
0
Try using the .html()
method to preserve tags:
尝试使用.html()方法来保留标记:
var legendStr = $(this).find('legendString').html();
Also note that .find('legendString')
will look for a tag called legendString
which probably is not what you are looking for. You probably need the :contains()
selector.
另请注意,.find('legendString')将查找名为legendString的标记,这可能不是您要查找的标记。您可能需要:contains()选择器。
#3
0
I don't think the function you need is in jQuery out-of-the-box. But you can write your own. Here is a starting point for you:
我不认为你需要的功能是jQuery开箱即用。但你可以写自己的。以下是您的出发点:
jQuery.extend(jQuery.fn, {
outerHtml: function(tagName) {
var matches = this.find(tagName);
var result = '';
for (var i=0; i<matches.length; i++) {
var el = matches[i];
result += '<' + tagName;
for (var i=0; i<el.attributes.length; i++) {
result += ' ';
result += el.attributes[i].name;
result += "='";
result += el.attributes[i].value;
result += "'";
}
if (el.innerHTML == null) { result += '/>'; }
else {
result += '>';
result += el.innerHTML;
result += '</'
result += tagName;
result += '>';
}
return result;
}
}});
}});
thus, you'll be able to do
因此,你将能够做到
var legendString = $(this).outerHtml('legendString');