prettyphoto wont load when writing href using document.write

此生再无相见时 提交于 2019-12-11 18:05:22

问题


i have some codes like this

<span id="$RsC" class="menu" title="Action menu" onclick="menu(\''.$RsC.'\')">click</span>

function menu(id){ 
var d = document.getElementById(id); 
d.innerHTML ='<a href="somthing;iframe=true&amp;width=400&amp;height=170"  rel="prettyPhoto[iframe]" >Doesnt work</a>';
}

<script type="text/javascript" charset="utf-8">
jQuery.noConflict();
jQuery(document).ready(function($) {
$(".pp_pic_holder").remove();
 $(".pp_overlay").remove();
 $(".ppt").remove();
$(".gallery a[rel^='prettyPhoto']").prettyPhoto({theme:'h0mayun'});
});
</script>

the problem is that after clicking on "click" and writing html link prettyPhoto plugin doesn't load

any help will be appreciated.

EDIT------------------- damn you conflict it was again jquery conflict but to got it work i change function to this:

<script type="text/javascript" charset="utf-8">
jQuery.noConflict();
jQuery(document).ready(function($) {
$(".pp_pic_holder").remove();
 $(".pp_overlay").remove();
 $(".ppt").remove();
$(".gallery a[rel^='prettyPhoto']").prettyPhoto({theme:'h0mayun'});
});
</script>

function menu(id){ 
var d = document.getElementById(id); 
d.innerHTML ='<a href="somthing;iframe=true&amp;width=400&amp;height=170"  rel="prettyPhoto[iframe]" >Doesnt work</a>';
jQuery.noConflict();
jQuery(document).ready(function($) {
$(".pp_pic_holder").remove();
 $(".pp_overlay").remove();
 $(".ppt").remove();
$(".gallery a[rel^='prettyPhoto']").prettyPhoto({theme:'h0mayun'});
});
}

回答1:


And it is not going to work this way. You initialize prettyPhoto for existing elements on the page after your page was loaded. When you insert a new element, prettyPhoto plugin knows nothing about it, because it already "attached" itself to the elements existed before. You have to initialize prettyPhoto after all changes on the page or attach it after changes to the updated elements. Like this

function menu(id){ 
var d = document.getElementById(id); 
d.innerHTML ='<a href="somthing;iframe=true&amp;width=400&amp;height=170"  rel="prettyPhoto[iframe]" >Doesnt work</a>';
$('a', d).prettyPhoto({theme:'h0mayun'});
}

ps: I've made a simple test with the example of prettyPhoto and it is working

function menu(id){ 
var d = document.getElementById(id); 
d.innerHTML ='<a href="images/fullscreen/3.jpg" rel="prettyPhoto[gallery1]">Test link</a>';
$('a', d).prettyPhoto();
}



回答2:


If you're doing this $(".gallery a[rel^='prettyPhoto']").prettyPhoto({theme:'h0mayun'}); before the onClick function.

You would have to do it again after you have written the html of the object so the plugin would initialize again.



来源:https://stackoverflow.com/questions/9010089/prettyphoto-wont-load-when-writing-href-using-document-write

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!