我的网站上有多个按钮,但无法将数据属性写入变量。
我的html
<button id="verzije" data-href="http://example.com/qjzvq6pxk1ru" data-sub="http://example.com/podnapisi/file.txt" data-vers="1">1. verzija</button>
<button id="verzije" data-href="http://example.com/f/4pXOzy" data-sub="http://example.com/podnapisi/file.txt" data-vers="2">2. verzija</button>
<button id="verzije" data-href="http://example.com/eo0hn7ajh4j3" data-sub="http://example.com/podnapisi/file.txt" data-vers="3">3. verzija</button>
我的 JavaScript
jQuery(document).ready(function(){
jQuery("button").click(function(e){
e.preventDefault();
var film = jQuery(this).data('href');
var pod = jQuery(this).data('sub');
var capback = jQuery(this).data('cap');
var verz = jQuery(this).data('vers');
jQuery.post("http://example.com/filmi.php",
{
link: film,
sub: pod,
cap: capback,
vers: verz
},
function(data){
jQuery('#flashplayer').html(data);
});
});
});
这段代码工作了一段时间,然后我在我的网站上添加了广告,代码停止工作。这段代码还有其他方法吗?我尝试解除广告事件监听器的绑定(bind),但没有成功。如果我打开广告拦截器,一切都会正常工作,如果关闭,则一切都不起作用。有什么解决办法吗?
注意:我包含了 jQuery!
最佳答案
您拥有的代码应该可以工作,尽管有一些报告称 JS 可能与 Google Ads 发生冲突。请参阅here和 here 。在链接到的案例中,window.onload
与 (document).ready
事件被两个函数重新绑定(bind)。
第二个链接说明了您可能的答案:
做
$(function() {
而不是
jQuery(document).ready(function() {
您还应该更改id
。
关于javascript - jQuery 在点击事件上 undefined variable ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27886845/