从div标签获取HTML内容

时间:2016-06-13 05:50:42

标签: javascript jquery

我想从我的变量中找到“Div”标记内容,例如“No More Events to Show”我该怎么做。

这是我存储在变量中的HTML内容。

var data="<div class="row events noevents text-center">
         No More Events to Show
     </div>"

提前致谢。

3 个答案:

答案 0 :(得分:1)

您可以将字符串转换为元素,然后从中选择元素。试试这个:

&#13;
&#13;
var data="<div class='row events noevents text-center'>No More Events to Show</div>"
var elements = $(data);
var str = $(elements).text();
alert(str);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

首先,您需要修复引号和多行字符串定义。然后创建一个DOM元素,然后获取.html().text()

&#13;
&#13;
var data = '<div class="row events noevents text-center"> No More Events to Show  </div>'
console.log($(data).html())
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

答案 2 :(得分:1)

var data="<div class='row events noevents text-center'>No More Events to Show</div>"


console.log($(data).html())
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

  1. 换入$()