[英]Change CSS on different page based on condition using Javascript/JQuery
在我的第一頁上,我有一個onclick事件,該事件將一個稱為Message的類添加到表單。
<input type="button" onclick="isDirty()" class="btn btn-default" />
<script>
function isDirty() {
$('form').addClass('Message');
}
</script>
然后我有一個if語句,它使用JQuery來檢查表單是否具有該類:
if ($('form').hasClass('Message')) {
$("#commentAndFileMessage").show();
}
如果是這樣,則“顯示:無;” 第2頁上的段落的css元素應更改為顯示:
<p id="commentAndFileMessage">You must save the new license on the details page.</p>
我對javascript有點陌生,但我想如果沒有Cookie或其他東西,這是不可能的。 有人知道這是否行得通嗎? 謝謝!
是的,如果您僅使用javascript / jquery,則不能執行此操作。請使用可以解決問題的javascript cookie。
您可以在下面的鏈接中找到完整的示例:
是的,使用html5 localStorage(幾乎支持所有現代瀏覽器),不使用cookie也是可能的。 在首頁上設置localStorage,代碼如下所示
function isDirty() { $('form').addClass('Message'); localStorage.setItem("form", "Message");//page1, set localstorage with form and messages as key-value pairs }
然后在第2頁上,從localstorage獲取此值
if(localStorage.getItem("form") == "Message") { $("#commentAndFileMessage").show(); }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.