【问题标题】:Jquery grab attr from automatically generated divJquery从自动生成的div中抓取attr
【发布时间】:2015-01-28 14:22:06
【问题描述】:

我创建了一个搜索引擎,但遇到了问题。

<div class="singleresult" onclick="benger()">
<img class="profilepic" src="http://graph.facebook.com/'+this.id+'/picture"  alt="Smiley face" height="50" width="50">
<p class="name">'+this.name+'</p> 
<p class="tapto" algin="right">TAP TO UNLOCK</p> 
</div>

如何通过点击div获取name值? 他们都有相同的类

【问题讨论】:

  • 我尝试添加 onclick 但它不起作用 $(this) 选择器
  • 分享你的实现代码
  • 小提琴不工作

标签: javascript jquery html children attr


【解决方案1】:

这将检索有关单击 .singleresult DIV 的特定文本:

$(document).on("click",".singleresult", function(){
    var name = $(this).find('.name').text();
    alert(name);
});

也就是说,听起来您想检索特定数据,不应该使用元素文本,而是使用任何自定义 data-* 属性。

【讨论】:

    【解决方案2】:

    使用 $(this) 选择器。

    <div class="test">test1</div>
    <div class="test">test2</div>
    <div class="test">test3</div>
    <div class="test">test4</div>
    <div class="test">test5</div>
    
    $('.test').click(function() {
       alert($(this).text());
    });  
    

    试试这个小提琴:http://jsfiddle.net/rfeuhyq0/

    【讨论】:

    • 它可以工作,但我需要从 div 类 singleresult 中的类名中获取值
    • 你的小提琴不工作。所以我们无法理解真正的问题。
      graph.facebook.com/'+this.id+'/picture" alt="笑脸" height="50" width="50">

      '+this.name+'

      点击解锁

      你需要哪个值
    【解决方案3】:

    尝试以下方法:

    $(document).ready(function(){
    $(document).on("click",".singleresult", function(){
    alert($(".name").text());
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-11
      • 2010-09-30
      • 1970-01-01
      • 2016-06-09
      相关资源
      最近更新 更多