weixin_33694172 2018-04-30 11:46 采纳率: 0%
浏览 119

jQuery中是否有Event Listeners?

在jQuery中,是否有Event Listeners可以被附加到每一个文字上?因此,当这个词被点击时,包括定义之类的信息可以显示在页面上。

这样做的目的是,在JSON文件中,当用户点击列表中的人名字时,屏幕右侧的数据框就会填充对艺术品位置的描述。

我的代码:

<!DOCTYPE html>
<hmtl lang="en">
    <head>
        <meta charset="utf-8" />
        <title>AJAX</title>
        <link rel="stylesheet" href="styles.css" type="text/css" />
        <script src="jquery.js" type="application/javascript"></script>
        <script src="ajax.js" type="application/javascript"></script>
    </head>
    <body>
      <div id="loaded-data"></div>
      <div id="result-box"></div>
    </body>
  </hmtl>

$(function() {

let request = $.ajax({
  method: 'GET',
  url : 'people.json',
  dataType: 'json',
});

request.done(function(data) {
  let list = data.body.list;
  let resultBox = $('#result-box');
  let unorderedList = $('<ul>');
  resultBox.append(unorderedList);

  for (let person of list) {
    let listItem = $('<li>');
    listItem.text(person.name);
    listItem.attr('data-url', person.links[0].href);
    unorderedList.append(listItem);
  }

});

request.fail(function(response) {
  console.log('ERROR: ' + response.statusText);

});

});

  {
  "links":[{"rel":"self","href":"http://www.philart.net/api/people.json"},{"rel":"parent","href":"http://www.philart.net/api.json"}],
  "head":{"title":"People","type":"listnav"},
  "body":{
  "list":[
  {"name":"Adam","links":[{"rel":"self","href":"http://www.philart.net/api/people/325.json"}]},
  {"name":"Abigail Adams","links":[{"rel":"self","href":"http://www.philart.net/api/people/157.json"}]},
  {"name":"John Adams","links":[{"rel":"self","href":"http://www.philart.net/api/people/410.json"}]},
  {"name":"Samuel Adams","links":[{"rel":"self","href":"http://www.philart.net/api/people/439.json"}]},
  {"name":"Lin Zexu","links":[{"rel":"self","href":"http://www.philart.net/api/people/347.json"}]},
  {"name":"James A. Zimble","links":[{"rel":"self","href":"http://www.philart.net/api/people/345.json"}]},
  {"name":"Doris Zimmerman","links":[{"rel":"self","href":"http://www.philart.net/api/people/171.json"}]}
  ]
  }
  }
  • 写回答

2条回答 默认 最新

  • weixin_33737134 2018-04-30 12:10
    关注

    Teemu has already mentioned a way to accomplish this behavior in the comment. You can do it as follows

    // handle click and add class
    $(".word").click(function() {
      var word = $(this).text()
      alert(word);
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div>
      <p class="word">Hello</p>
    </div>

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 python忆阻器数字识别
  • ¥15 无法输出helloworld
  • ¥15 高通uboot 打印ubi init err 22
  • ¥20 PDF元数据中的XMP媒体管理属性
  • ¥15 R语言中lasso回归报错
  • ¥15 网站突然不能访问了,上午还好好的
  • ¥15 有没有dl可以帮弄”我去图书馆”秒选道具和积分
  • ¥15 semrush,SEO,内嵌网站,api
  • ¥15 Stata:为什么reghdfe后的因变量没有被发现识别啊
  • ¥15 振荡电路,ADS仿真