简体   繁体   English

点击jquery加载html模板

[英]load html template on click jquery

have main html template and sub html template. 有主html模板和子html模板。

<button id="btn">Click Me...</button>

Main HTML: 主要HTML:

<div id="main">
 <label>Main</label>
</div>

Sub HTML: 子HTML:

<label>sub</label>

JQuery: JQuery的:

$(document).on("click", "#btn", function(e) {
            e.preventDefault();
            $("#main").load("main.html");


        });

Scenario trying to achieve is I have a common button in all the pages. 尝试实现的方案是我在所有页面上都有一个通用按钮。 In default page ie Main.html I will show its label. 在默认页面(即Main.html)中,我将显示其标签。 once on click of the button I want to replace the label of main.html with sub.html, Again on click of the button I need to toggle it with the default label. 一次单击按钮,我想用sub.html替换main.html的标签,再次单击按钮,我需要使用默认标签进行切换。

Can anyone help me out how to achieve this. 任何人都可以帮我解决这个问题。

i think this Fiddle is what the OP wants to achieve 我认为这个小提琴是OP想要实现的目标

loading the html files alternatively : 加载html文件:

var loadMain = true;
var loadSub = false;
$(document).on("click", "#btn", function(e) {
            e.preventDefault();
    if(loadMain){
            $("#main").load("main.html");
        alert("HERE");
            loadSub = true;
            loadMain = false;
    }else{
         $("#main").load("sub.html");
        alert("THERE");
            loadMain = true;
            loadSub = false;
    }

});

You can change label text with jquery: Try this: 您可以使用jquery更改标签文本:尝试以下操作:

$(document).on("click", "#btn", function(e) {

            $("#main").load("main.html");

        if ($("#expanderSign").text() == "Main"){
            $("#expanderSign").html("sub")
        }
        else {
            $("#expanderSign").text("Main")
        }

        });

http://jsfiddle.net/wcd7N/ http://jsfiddle.net/wcd7N/

Here's a fiddle with your code: 这是您的代码的摆弄:

$(document).on("click", "#btn", function (e) {
  e.preventDefault();
  $("#main").load("/echo/html/", {html: "<label>sub</label>"});
});

http://jsfiddle.net/cnnhg/2/ http://jsfiddle.net/cnnhg/2/

The only difference with yours is you don't need that data argument, you use a file. 与您唯一的区别是您不需要该数据参数,而使用文件。 Something else is wrong, like adding the onclick before the button is ready. 还有其他问题,例如在按钮准备就绪之前添加onclick。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM