我有一个 AJAX 调用,可以查找任何具有 videoReplace
类的元素
jQuery('.videoReplace').each(function(index) {
jQuery.ajax({
type: 'post',
url: 'http://optinsmart.com/videos.php',
cache: false,
data:{
'video': jQuery( this ).attr('video-source')
},
dataType: 'html',
success:function(data){
jQuery('#'+jQuery( this ).attr('id')).html(data);
},
error: function(errorThrown){
console.log(errorThrown);
}
});
});
这是我的 HTML
<h2>Tanya & Matt Ice Cream</h2>
<div id="tanya" id-name="tanya" class="videoReplace" video-source="Opt-in Smart review by Tanya_Matt Ice cream"></div>
理论上,这段代码应该发布到我的视频页面,并且播放的视频会以 html 形式发回。然后我想将 html 添加到我正在使用的 div 中。
它告诉我 jQuery('#'+jQuery( this ).attr('id'))
未定义
有什么建议吗?
最佳答案
您的问题是 success
回调中的 this
与 each< 上下文中的
回调。 this
不同
关于javascript - AJAX 成功后更改 Element 的 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22274573/