[英]Why JQuery is not working in JSF (Primefaces)
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:composite="http://java.sun.com/jsf/composite"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.prime.com.tr/ui">
<h:head>
<script src="/resources/js/jquery-1.7.2.js" ></script>
<script >
$j=jQuery.noConflict();
function appendText() {
alert('hi');
var txt1 = "<p>Text.</p>";
var txt2 = $j("<p></p>").text("Text.");
var txt3 = document.createElement("p");
txt3.innerHTML = "Text.";
$j("p").append(txt1, txt2, txt3);
}
function appendList() {
var item1 = "<li>List item</li>";
var item2 = $j("<li></li>").text("List item");
var item3 = document.createElement("li");
item3.innerHTML = "List item";
$j("ol").append(item1, item2, item3);
}
</script>
</h:head>
<h:body>
<f:verbatim>
<p>This is a paragraph.</p>
<ol>
<li>List item 1</li>
<li>List item 2</li>
<li>List item 3</li>
</ol>
<button id="btn1" onclick="appendText()">Append text</button>
<button id="btn2" onclick="appendList()">Append list items</button>
</f:verbatim>
<h:form>
<h:commandButton value="jsf cmd btn APPEND TEXT" onclick="appendText()" id="btn3"/>
</h:form>
</h:body>
</html>
這是一個簡單的JSF頁面,腳本中包含JQuery代碼。
當我運行時,頁面正確加載。 但是,它並沒有調用JQuery腳本。
我不知道為什么......任何身體都可以建議我做錯了什么?
謝謝你。
您應該刪除包含jQuery的腳本標記。
Primefaces與jQuery捆綁在一起,因此如果您在web.xml配置文件中正確配置Primefaces Resources servlet,那么它將自動包含在您的所有網頁中。
編輯:
此外,您不能使用$字符在JSF頁面上調用jQuery,因為這是表達式的保留字符。 你必須以長篇形式調用jQuery ...
jQuery('.example').click();
編輯2:
另外,盡量不要同時將Javascript與單引號和雙引號混合使用。
alert('hi');
var txt1 = "<p>Text.</p>";
選擇一個引用字符並堅持下去。 這是一種很好的做法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.