获取元素内的段落文本 [英] Get paragraph text inside an element

查看:61
本文介绍了获取元素内的段落文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在< li> 元素中包含< p> 的文本值。

I want to have the text value from a <p> inside a <li> element.

html

<ul>
  <li onclick="myfunction()">
    <span></span>
    <p>This Text</p>
  </li>
</ul>

javascript

function myfunction() {
  var TextInsideLi = [the result of this has to be the text inside the paragraph"];
}

如何做到这一点?

推荐答案

或者,您也可以将li元素本身传递给myfunction函数,如下所示:

Alternatively, you can also pass the li element itself to your myfunction function as shown:

function myfunction(ctrl) {
  var TextInsideLi = ctrl.getElementsByTagName('p')[0].innerHTML;
}

并在您的HTML中,< li onclick =myfunction(this)>

and in your HTML, <li onclick="myfunction(this)">

这篇关于获取元素内的段落文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆