如何在Div中选择具有相同类但多个div的文本

时间:2016-08-27 06:34:05

标签: javascript jquery

我需要你的帮助。在获取某些工作方面,我尝试从提要和博客中提取信息,但它不受信任。 这是我的代码......

<div class="select_obj">
    <h4>Promote Channel</h4>
</div>
<div class="select_obj">
    <h4>Increase Conversion on Site</h4>
</div>
<div class="select_obj">
    <h4>Increase Conversion on App</h4>
</div>
...
<div class="select_obj">
    <h4>Get Video View</h4>
</div>

如何在单击h4中选择并获取文本。 我只是不知道如何开始,我需要你们的帮助。感谢。

4 个答案:

答案 0 :(得分:6)

使用javascript

function getTxt(x){
    alert(x.innerHTML);  
}
<div class="select_obj">
    <h4 onclick="getTxt(this);">Promote Channel</h4>
</div>
<div class="select_obj">
    <h4 onclick="getTxt(this);">Increase Conversion on Site</h4>
</div>
<div class="select_obj">
    <h4 onclick="getTxt(this);">Increase Conversion on App</h4>
</div>
...
<div class="select_obj">
    <h4 onclick="getTxt(this);">Get Video View</h4>
</div>

使用jquery的简单解决方案

$(".select_obj").click(function(){
    //to get the content  
    var txt=$(this).children("h4").text();
    alert(txt);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="select_obj">
    <h4>Promote Channel</h4>
</div>
<div class="select_obj">
    <h4>Increase Conversion on Site</h4>
</div>
<div class="select_obj">
    <h4>Increase Conversion on App</h4>
</div>
...
<div class="select_obj">
    <h4>Get Video View</h4>
</div>

答案 1 :(得分:3)

使用jQuery你可以这样做

$('h4').on('click',function(event){
alert($(this).text());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="select_obj">
  <h4>Promote Channel</h4>
</div>
<div class="select_obj">
  <h4>Increase Conversion on Site</h4>
</div>
<div class="select_obj">
  <h4>Increase Conversion on App</h4>
</div>
<div class="select_obj">
  <h4>Get Video View</h4>
</div>

使用原生javascript

var getAllH4 = document.querySelectorAll('h4');
getAllH4.forEach(function(item, index) {
  (function(item, i) { // creating closure
    item.addEventListener('click', function() {
      alert(item.textContent);
    });
  }(item, index))
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="select_obj">
  <h4>Promote Channel</h4>
</div>
<div class="select_obj">
  <h4>Increase Conversion on Site</h4>
</div>
<div class="select_obj">
  <h4>Increase Conversion on App</h4>
</div>
<div class="select_obj">
  <h4>Get Video View</h4>
</div>

答案 2 :(得分:0)

这是我的代码.....我给他们id并用函数发送clicked元素的id返回你点击它的文本...    

          <h4 id="a" onclick="getText(id)">Promote Channel</h4>
        </div>
        <div  class="select_obj">
          <h4 id="b" onclick="getText(id)">Increase Conversion on Site</h4>
        </div>
        <div  class="select_obj">
          <h4 id="c" onclick="getText(id)">Increase Conversion on App</h4>
        </div>
        ...
        <div  class="select_obj">
          <h4 id="d" onclick="getText(id)">Get Video View</h4>
        </div>

确定使用javascript .....

    <script>
            function getText(id)
            {
                var textReturned ="";
                textReturned = document.getElementById(id).innerHTML;
                alert(textReturned);
            }
        </script>

答案 3 :(得分:0)

试试此代码

$('h4').on('click',function(){alert($(this).text()) ;});