Get paragraph text inside an element

后端 未结 9 512
孤独总比滥情好
孤独总比滥情好 2021-02-03 19:21

I want to have the text value from a

inside a

  • element.

    html:

  • 相关标签:
    9条回答
    • 2021-02-03 20:12

      HTML:

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

      JavaScript:

      function myfunction(foo) {
          var elem = foo.getElementsByTagName('p');
          var TextInsideLi = elem[0].innerHTML;
      }​
      
      0 讨论(0)
    • 2021-02-03 20:15

      change your html to the following:

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

      then you can get the content of your paragraph with the following function:

      function getContent() {
          return document.getElementById("myParagraph").innerHTML;
      }
      
      0 讨论(0)
    • Use jQuery:

      $("li").find("p").html()
      

      should work.

      0 讨论(0)
    提交回复
    热议问题