[英]Javascript hyperlinks don't work with jQuery append
I need some help pointing to the right direction. 我需要一些指向正确方向的帮助。 I have a jQuery code what pulls some HTML content via the
$.get
function after the page is loaded, puts them into the $data
variable and it's appended to the div_content
. 我有一个jQuery代码,在页面加载后通过
$.get
函数提取一些HTML内容,将它们放入$data
变量中,并将其附加到div_content
。
Everything works perfectly, except after the appending the javascript links in the original content don't work. 一切正常,除非在原始内容中附加了javascript链接后不起作用。
The code part: 代码部分:
$(document.body).ready(function() {
$.get("content1.php", {id:"1234" }, function(data) {
// for example this is the pulled data
var data = '<a href="javascript:;" data-id="someotherpage.php?var1=a&var2=b&var3=c">link</a>'
$('.div_content').append(data);
});
});
The standard tags, without javascript aren't affected, they work fine. 没有javascript的标准标签不会受到影响,它们可以正常工作。
I found some advices like this - jQuery Appended elements with href and javascript doesn't work - and this - Appending a link with Jquery - and read the jQuery's .on() function but doesn't seem to resolve my exact problem with appending the content. 我发现了这样的建议- 带href和javascript的jQuery追加元素不起作用 -并且- 带Jquery的链接 -并读取了jQuery的.on()函数,但似乎并不能解决我的确切问题内容。
I have jQuery 1.10.1, thanks for all the inputs. 我有jQuery 1.10.1,感谢所有输入。
Without seeing what data may contain its difficult to see exactly what you're trying to achieve but I can see the fist thing you do after receiving the ajax response is overwrite the returned data which is obviously incorrect. 没有看到什么数据可能很难准确地知道您要实现的目标,但是我可以看到收到ajax响应后您要做的第一件事就是覆盖返回的数据,这显然是不正确的。
perhaps you meant something like this?: 也许您的意思是这样的:
$.get("content1.php", {id:"1234" }, function(i,data) {
var link = '<a href="someotherpage.php?var1='+data.var1+'&var2='+data.var2+'">link</a>';
$('.div_content').append(link);
});
Thanks for the responses and comments, mea culpa, it was my, a design error. 感谢您的答复和评论,这是我的设计错误。 I had to include the jquery and fancybox JS and CSS into the someotherpage.php (the data-id attribute), and now it's working.
我必须将jquery和fancybox JS和CSS包含到someotherpage.php(data-id属性)中,现在它可以工作了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.