簡體   English   中英

單擊按鈕加載不同的html頁面

[英]Load different html page on button click

我想在按鈕單擊時加載不同的html頁面,這是我的主頁代碼

<body>

<div id="header">    
<div id="radio">
  <input type="radio" id="navmenu1" name="radio"><label for="navmenu1">Home</label>
  <input type="radio" id="navmenu2" name="radio"><label for="navmenu2">Contact</label>
  <input type="radio" id="navmenu3" name="radio"><label for="navmenu3">Resume</label>
</div>

</div><!--head-->

<div id="content-area">
<div id="content"></div>    
</div>

<div id="footer-row">
  <footer>
     <div id="footer-row-1">
        <ul id="footer">
            <li>&copy 2013 by Han Chang</li>
        </ul>
    </div>
    <div id="footer-row-2">        
    </div>
</footer>
</div>

</body>

我的home.js代碼如下

$(function () {
$("#radio").buttonset();   
$("#radio :radio").click(function (e) {
    var $val = $("#" + $(this).attr("for")).val();
    alert($val);
});

我想知道我選擇哪個按鈕,然后加載其html頁面。 我怎樣才能做到這一點 ? 謝謝

您可以使用window.location。 假設輸入/錨定/按鈕具有創建所需URL的上下文。

window.location = 'http://your.url.com'

對每個按鈕使用onclick事件來調用js函數。

<input type="radio" id="navmenu1" name="radio" onclick="someFunction()"><label for="navmenu1">Home</label>

您可以向該函數發送字符串或其他內容來確定單擊了哪個按鈕。 您調用的函數可以通過使用window.location重定向到其他html頁面

現在有了值,您可以添加邏輯,例如:

if (val === "Option 1") {
    // Action here
}

聽起來您想更改頁面上的某些內容,這是一個簡單的JQuery函數:

$("body").replaceWith(//HTML here);

有關該功能的更多信息: http : //api.jquery.com/replaceWith/

因此,完整的示例如下所示:

if (val === "Option 1") {
    $("body).replaceWith("<body><h2>Option 1</h2></body>");
}

或任何你喜歡的。

您可以使用AJAX將新的HTML加載到頁面中。 您可以具有一組HTML頁面並將其加載到頁面中。

您可以使用jquery模板插件或通過jquery和httphandler獲取html

要獲取單選按鈕的值,您需要具有一個value屬性。

http://jsfiddle.net/TpmTt/

然后,您可以使用window.location,window.navigate檢查並重定向用戶

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM