gpt4 book ai didi

javascript - jQuery 将 html 解析为 JSON

转载 作者:行者123 更新时间:2023-11-30 10:28:40 24 4
gpt4 key购买 nike

我目前正在使用以下代码:

jQuery('#book-a-service').click(function(){
var selectedServices = jQuery('.selected').parent().parent().html();
console.log(selectedServices);
});

然后返回:

<td rowspan="3">Brakes</td>
<td class="service-title">BRAKES SET</td>
<td class="service-description"><p>Setting of front and rear brakes for proper functioning (excluding bleeding)</p></td>
<td class="service-price">R <span id="price-brakes-set">R75</span><div id="select-brakes-set" class="select-service selected"></div>
</td>

这就是我想要的,除了我需要一个包含 JSON 中“.selected”类的所有元素的数组。我只是想知道我如何几乎可以以一种只获取的内容的方式解析它td 标签和“服务价格”只有数值,然后我如何将这些值插入到 json 对象中?

非常感谢任何帮助..

最佳答案

jQuery 不是我最强大的框架,但这似乎可以解决问题。

jQuery('#book-a-service').click(function(){
var selected = jQuery('.selected');
selected.each( function() {
var children = jQuery(this).parent().parent().find('td');
var json = {};
console.log(children);
json.type = jQuery(children[0]).text();
json.title = jQuery(children[1]).text();
json.description = jQuery(children[2]).find('p').text();
json.price = jQuery(children[3]).find('span#price-brakes-set').text();
console.log(json);
console.log(JSON.stringify(json));
});
});

在行动中:http://jsfiddle.net/3n1gm4/DmYbb/

关于javascript - jQuery 将 html 解析为 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18260831/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com