0

我在服务器端创建了一个ul列表,如下所示。我想用 jquery 向a元素添加点击事件,但我无法访问任何元素。这是我的代码:

服务器端:

 StringBuilder sbSubCitchens = new StringBuilder();

 sbSubCitchens.Append(@"<div id=""content_1"" class=""subCats"">");
 sbSubCitchens.Append("<ul>");

foreach (kitchen kitchen in kitchenList)
 {
    sbSubCitchens.Append(@"<li><a  class="" " + kitchen.KitchenId + @""">" + kitchen.Name + "</a>  </li>");
 }
 sbSubCitchens.Append("</ul>");
 sbSubCitchens.Append("</div>");

 ltrKitchenList.Text = sbSubCitchens.ToString();

Javascript:

$(document).ready(function () {

 $(".mCSB_container ul li a").click(function () {
       // do smth...
    });
})

html输出:

<div class="mCSB_container" style="position: relative; top: 0px;">
  <ul>
   <li><a class=" 1">Cafe</a>  </li>
   <li><a class=" 2">Dünya Mutfağı</a>  </li>
  </ul>
</div>
4

2 回答 2

3

Try this:

$(".mCSB_container ul li a").click(function (e) {
    e.preventDefault();
    // do smth...
});

I think the click may be working, but since you aren't preventing the default action, the link may be reloading the page.

Also make sure the html output is what you expect, since the server-side code you posted, by itself, won't give you the output you posted (and therefore the selector wouldn't work).

于 2013-09-16T13:38:53.760 回答
0

我认为您必须输入“href”参数:

<a href="#" class="whatever">Some stuff</a>
于 2013-09-16T15:43:12.797 回答