javascript - 获取元素内的段落文本

标签 javascript html paragraph

我想从 <p> 中获取文本值在 <li> 里面元素。

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 函数,如下所示:

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

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

关于javascript - 获取元素内的段落文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11633951/

相关文章:

html - 图像对齐占段落的 100%

javascript - 使用 jquery 简单验证按钮单击操作

用于带重音符号的大写字母的 JavaScript 正则表达式

javascript - html 为在每个浏览器中正常工作的每个选项选择不同的背景颜色?

javascript - 防止鼠标中键在 anchor 标签中打开新标签

html - 垂直对齐 p 标签到中间

javascript - Jquery-ui tabs (ajax) .... 重新选择选项卡时停止选项卡重新加载 url

javascript - 在运行时加载 css

javascript - 我希望在网络浏览器中生成随机散布在某个区域上的多个图像

javascript获取网页中选定文本的段落