动态添加监听到阿贾克斯jQuery中创建的内容动态、内容、到阿贾克斯、jQuery

2023-09-10 13:48:37 作者:对着背影祝福你

我想获得一个链接点击HTML值。 这些链接是动态创建使用Ajax,所以我不认为.bind会 工作,我没有最新的版本,.live

  $('#DIV消息)。点击(函数(){
  VAR valueSelected = $(本)。html的(); //拿起整个ID。我juust想单HREF!
  警报(v​​alueSelected);
  返回false;
});



< D​​IV ID =消息>
< BR />
<一类=doYouMean的href =#>位置A< / A>
< BR />
&其中;一类=doYouMean的href =#>位置B&所述; / a取代;
< BR />
&其中;一类=doYouMean的href =#>位置℃下/ a取代;
< BR />
&其中;一类=doYouMean的href =#>位置D&所述; / a取代;
< BR />
&其中;一类=doYouMean的href =#>位置E&所述; / a取代;
< BR />
< / DIV>
 

解决方案

应用处理程序,只是链接,在AJAX负荷的回调。

  $('#DIV消息)。载荷(myUrl,函数(){
    $('#DIV消息)。点击(函数(){
       VAR valueSelected = $(本)。html的();
       警报(v​​alueSelected);
       返回false;
    });
});
 

jquery 监听td点击事件 jQuery中的属性,文档,动画以及事件

I am trying to get the html value of a linked clicked. The links are created dynamically with Ajax so I don't think .bind will work and I don't have latest version with .live

$('div#message').click(function() {
  var valueSelected = $(this).html();  // picks up the whole id. I juust want single href!          
  alert(valueSelected);
  return false;
});



<div id="message">
<br/>
<a class="doYouMean" href="#">location A</a>
<br/>
<a class="doYouMean" href="#">location B</a>
<br/>
<a class="doYouMean" href="#">location C</a>
<br/>
<a class="doYouMean" href="#">location D</a>
<br/>
<a class="doYouMean" href="#">location E</a>
<br/>
</div>

解决方案

Apply your handler to just the links, in the callback of the AJAX load.

$('div#message').load( myUrl, function() {
    $('div#message a').click(function() {
       var valueSelected = $(this).html();
       alert(valueSelected);
       return false;
    });
});