Javascript 将href链接添加到<;李>;使用jQuery的项

Javascript 将href链接添加到<;李>;使用jQuery的项,javascript,jquery,Javascript,Jquery,我对jQuery非常陌生,我正在寻找一种方法来执行以下操作: 我有一个项目清单: $.get('../getContent', function(responseJson) { var $ul = $('<ul>').appendTo($('#content')); $.each(responseJson, function(index, item) { $('<li>') .text(item)

我对jQuery非常陌生,我正在寻找一种方法来执行以下操作:

我有一个项目清单:

  $.get('../getContent', function(responseJson) { 
      var $ul = $('<ul>').appendTo($('#content'));  
      $.each(responseJson, function(index, item) { 
        $('<li>')
            .text(item)
            .appendTo($ul);
      });
  });
$.get('../getContent',函数(responseJson){
var$ul=$(“
    ”).appendTo($(“#content”); $.each(responseJson,函数(索引,项){ $(“
  • ”) .文本(项目) .appendTo($ul); }); });
我想让每个列表项都可以点击,点击后我想把它的文本发送到一个servlet


有人能告诉我怎么做吗?谢谢:)

只需添加一个单击处理程序即可

$.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() );
            });
      });
  });
$.get('../getContent',函数(responseJson){
var$ul=$(“
    ”).appendTo($(“#content”); $.each(responseJson,函数(索引,项){ $(“
  • ”) .文本(项目) .appendTo($ul) 。单击(函数(){ /*用servlet代码替换警报*/ 警报($(this.text()); }); }); });
$(“
  • ”) .文本(项目) 。单击(函数(){ $.post(url,{text:item}); }) .appendTo($ul);

  • 其中
    url
    是servlet的url,假设您正在向其发布
    text
    变量。

    谢谢。你知道有什么方法可以使用我在css中指定的链接格式吗?当然可以:
    $('
  • $('<li>')
            .text(item)
            .click(function() {
                $.post(url, {text: item});
            })
            .appendTo($ul);