如何从 jquery 或 javascript 中获取 HTML 的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11408466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to get value of HTML from jquery or javascript
提问by kitokid
I want to select the following three values from the HTML file either by Jquery or Javascript.
我想通过 Jquery 或 Javascript 从 HTML 文件中选择以下三个值。
- class "class1" href value
- class "class1" inner text value (PersonA in the example code)
- class "Title" inner text value (Accountant in the example)
- 类“class1”href 值
- 类“class1”内部文本值(示例代码中的 PersonA)
- 类“标题”内部文本值(示例中的会计)
How can I select all the data of li node by node as? I am lost :(
如何逐个节点选择li节点的所有数据?我搞不清楚了 :(
<ol id="result-set">
<li id="v-0">
<div class="result-data">
..
<h2>
<a class="class1" href="">PersonA</a>
</h2>
<dl class="basic">
<dt>Title</dt>
<dd class="title">Accountant</dd>
....
</dl>
</div>
</li>
<li id="v-1">
...
</li>
.....
回答by nnnnnn
With jQuery, you can do something like this:
使用 jQuery,您可以执行以下操作:
$("#result-set li").each(function() {
var $currentLi = $(this),
$class1link = $currentLi.find("a.class1"),
class1href = $classAlink.attr("href"),
class1content = $classAlink.html();
// do something with values
});
The .each()
method will process each li element. Within the callback to .each()
the variable $currentLi
is a jQuery object holding that li (set from $(this)
where this
is the li element itself). The .find()
method is used to find the anchor element within the li and then its href and content are retrieved.
该.each()
方法将处理每个 li 元素。在对.each()
变量的回调中$currentLi
是一个持有该 li 的 jQuery 对象(从$(this)
哪里设置this
li 元素本身)。该.find()
方法用于在 li 中查找锚元素,然后检索其 href 和内容。
The "Accountant" you asked about is one item in a definition list, so you'd probably want to loop through that list with another .each()
statement nested inside the one above.
您询问的“会计”是定义列表中的一项,因此您可能希望使用.each()
嵌套在上面的语句中的另一条语句遍历该列表。
You don't make it clear how you want to use the values, but this should get you started. For further details about the various jQuery methods I've mentioned check the jQuery API.
您没有明确说明您想如何使用这些值,但这应该可以帮助您入门。有关我提到的各种 jQuery 方法的更多详细信息,请查看jQuery API。
回答by Scotty
To get "PersonA": $('#v-0 h2 a').html();
获取“PersonA”: $('#v-0 h2 a').html();
To get href of that link: $('#v-0 h2 a').attr('href');
要获取该链接的 href: $('#v-0 h2 a').attr('href');
To get "Accountant": $('#v-0 dl dd').html();
要获得“会计师”: $('#v-0 dl dd').html();
You can modify the id ("v-0") at the start of the selector to choose a particular "row" of your data set.
您可以修改选择器开头的 id(“v-0”)以选择数据集的特定“行”。
回答by F0G
document.getElementById(Id).value
returns value of element with specific id. in jquery:
返回具有特定 id 的元素的值。在jQuery中:
$("#id").val()
by class $(".yourClass").val()
按班级 $(".yourClass").val()
to get attribute value use attr("attributeName")
for example $(".class1").attr('href')
.
获取属性值使用attr("attributeName")
例如$(".class1").attr('href')
。
if you want to get text from specified element use .text()
like $(".title").text() //will return Accountant
.
如果您想从指定元素中获取文本,请使用.text()
类似$(".title").text() //will return Accountant
.
回答by Undefined
You mean selecting them with a jQuery selector? That would be done like so:
你的意思是用 jQuery 选择器选择它们?这样做会像这样:
$('.class1').attr('href') //class1 href, i persume you dont mean classA as it doesnt exist in your code
$('.class1').text(); //PersonA text using the same selector
$('.title').text(); //Accountant from the .title dd