用jQuery提取链接的文本

时间:2014-09-17 15:44:54

标签: jquery

我有以下代码:

var code = '<span class="category-str-list">
<a href="link1.html">Spas</a>,<a href="link2.html">Massage</a></span>';

如何获得以下字符串?

var activities = 'Spas, Massage';

1 个答案:

答案 0 :(得分:3)

只需将其解析为HTML并获取文本内容?

var activities = $(code).text();

&#13;
&#13;
var code = '<span class="category-str-list"><a href="link1.html">Spas</a>,<a href="link2.html">Massage</a></span>';

var activities = $(code).text();

$('body').html(activities)
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
&#13;
&#13;
&#13;