[英]add a href-link to <li> items with jQuery
I'm very new to jQuery and I'm looking for a way to do the following: 我是jQuery的新手,我正在寻找一种方法来执行以下操作:
I have a list of Items: 我有一个项目列表:
$.get('../getContent', function(responseJson) {
var $ul = $('<ul>').appendTo($('#content'));
$.each(responseJson, function(index, item) {
$('<li>')
.text(item)
.appendTo($ul);
});
});
I want to make each list item clickable and on click I want to send it's text to a servlet. 我想让每个列表项都可单击,然后单击我想将它的文本发送到servlet。
Can someone tell me how to do this? 谁能告诉我怎么做? Thanks :)
谢谢 :)
$('<li>')
.text(item)
.click(function() {
$.post(url, {text: item});
})
.appendTo($ul);
Where url
is the url of your servlet, and supposing you're posting to it the text
variable. url
是你的servlet的url,并假设你正在向它发布text
变量。
Just add a click handler to it 只需添加一个点击处理程序即可
$.get('../getContent', function(responseJson) {
var $ul = $('<ul>').appendTo($('#content'));
$.each(responseJson, function(index, item) {
$('<li>')
.text(item)
.appendTo($ul)
.click(function(){
/* replace alert with servlet code*/
alert( $(this).text() );
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.