Javascript 任何事件侦听器
是否有可以附加到单词的事件侦听器。因此,单击单词时,页面上可以显示定义等信息。使用jQuery 谢谢, 亚当 很抱歉没有发布代码。我必须这样做,当用户点击列表中某个人的名字时,屏幕右侧的数据框中会充满对艺术品位置的描述。在我的JSON文件中 这是到目前为止我的代码Javascript 任何事件侦听器,javascript,jquery,json,ajax,event-handling,Javascript,Jquery,Json,Ajax,Event Handling,是否有可以附加到单词的事件侦听器。因此,单击单词时,页面上可以显示定义等信息。使用jQuery 谢谢, 亚当 很抱歉没有发布代码。我必须这样做,当用户点击列表中某个人的名字时,屏幕右侧的数据框中会充满对艺术品位置的描述。在我的JSON文件中 这是到目前为止我的代码 <!DOCTYPE html> <hmtl lang="en"> <head> <meta charset="utf-8" /> <titl
<!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"}]}
]
}
}
AJAX
$(函数(){
让请求=$.ajax({
方法:“GET”,
url:'people.json',
数据类型:“json”,
});
请求完成(功能(数据){
let list=data.body.list;
设resultBox=$(“#结果框”);
让无序列表=$(“”);
resultBox.append(无序列表);
为(名单上的人){
让listItem=$(“- ”);
listItem.text(person.name);
listItem.attr('data-url',person.links[0].href);
无序列表。追加(listItem);
}
});
请求失败(功能(响应){
console.log('ERROR:'+response.statusText);
});
});
{
“链接”:[{“rel”:“self”,“href”:”http://www.philart.net/api/people.json},{“rel”:“parent”,“href”:http://www.philart.net/api.json"}],
“head”:{“title”:“People”,“type”:“listnav”},
“正文”:{
“名单”:[
{“name”:“Adam”,“links”:[{“rel”:“self”,“href”:”http://www.philart.net/api/people/325.json"}]},
{“名称”:“阿比盖尔·亚当斯”,“链接”:[{“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"}]},
{“姓名”:“林则徐”,“链接”:[{“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"}]}
]
}
}
已经在评论中提到了实现此行为的方法。你可以这样做
//句柄单击并添加类
$(“.word”)。单击(函数(){
var word=$(this.text())
警惕(word);
});代码>
你好
您可以替换该单词并用div将其包装起来
$('p').html(function(index, value) {
return value.replace(/\b(here)\b/g, '<div class ="event">here</div>');
});
$('.event').click(function() {
console.log('definition');
});
$('p').html(函数(索引,值){
返回值。替换(/\b(此处)\b/g,“此处”);
});
$('.event')。单击(函数(){
console.log('definition');
});
这是信息:单击此处。
请显示您的代码。。。所以我们可以帮助您不,您不能将事件侦听器附加到单词,但是,您可以将单个单词包装到元素,然后将单击侦听器附加到该元素。