今天我一直在試圖解決這個問題。我已達到(以某種方式)解決方案,但未按預期工作。當FancyBox在最初不渲染的容器內時,無法使FancyBox正常工作
我是新進入jQuery的世界,所以我會盡力澄清。 在我的示例中,我有一個xhtml頁面,其中包含另一個xhtml頁面,其中包含 <ui:include>
標記。包含的頁面不是最初呈現的(由於包含所有圖片)。當它通過<p:commandLink>
呈現時,頁面會顯示所有圖片正確。每張圖片都是一個<h:ouputLink>
,它顯示了一個fancyBox,但是行爲有點煩人。
當包含的頁面最終呈現時,我第一次點擊<h:ouputLink>
fancyBox不顯示,但隨後的點擊打開fancyBox,它工作正常。我不明白爲什麼。我使用jsf2,PrimeFaces 3.02,FancyBox 1.7。 我的代碼:
的main.xhtml
<--Main xhtml-->
<h:form id="myForm">
<h:panelGrid id="panelGridMain"
rendered="#{myBean.actualPage eq 'main'}">
<p:commandLink
id="showIncludedPage"
actionListener="#{myBean.setPage('test')}"
update="@form">
<h:graphicImage value="/Image/TbnPic1.jpg"/>
</p:commandLink>
</h:panelGrid>
<h:panelGroup id="myTestPanel" rendered="#{myBean.actualPage eq 'test'}">
<ui:include src="test.xhtml" />
</h:panelGroup>
</h:form>
Test.xhtml
<ui:component xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<h:outputLink value="/Images/Pic1.jpg" id="example1" title="" >
<h:graphicImage alt="example1" value="/Images/TbnPic1.jpg" />
</h:outputLink>
</ui:component>
最後我jQuery代碼
$(document).ready(
function() {
$("#myForm\\:example1").live("click",function(e) {
alert(e.isDefaultPrevented());
if (! e.isDefaultPrevented()){
e.preventDefault();
}
$(this).fancybox();
});
});
一些aclarations:
*我紅色,推薦.on()
,但它不適合我,我需要做更多的測試。
*我必須添加e.preventDefault()到我的.js因爲當我點擊鏈接彈出fancyBox,鏈接重定向到包含照片的URL(例如:localhost:8080 /。 ./Images/Pic1.jpg)
*如果頁面最初呈現fancyBox工作正常。
感謝您的幫助,我希望你能明白這個問題=)
拍手,我需要更多的練習才能找到像這樣的東西。再次感謝這一點以及所有提示。 – Camauu 2012-03-01 03:01:42
不客氣。 – BalusC 2012-03-01 03:05:20