简体   繁体   English

单击javascript加载特定的div?

[英]loading a specific div upon click in javascript?

I have no idea as to how to ACTUALLY word this, so this might be confusing, sorry. 我不知道该如何正确说出这个字,对不起,这可能会造成混淆。

I have a page here: http://dl.dropbox.com/u/3264697/calc/v2/index.html 我在这里有一个页面: http : //dl.dropbox.com/u/3264697/calc/v2/index.html

At the top, you see a menu, with several different functions - each on their own seperate page. 在顶部,您会看到一个菜单,其中包含几个不同的功能-每个功能都位于各自的单独页面上。 It can be represented as such: 可以这样表示:

A | A | B | B | C | C | D d

where | 在哪里 denotes a page, 表示一页,

ALL of the functions USED to be on one page, as such: 所有使用的功能都在一页上,例如:

A + B + C + D A + B + C + D

A was visible when the link was clicked, and B,C and D were triggered upon clicking them. 单击链接时可见A,单击它们便触发B,C和D。

clicking on one of those functions would load that div and hide the current one - so one could switch back and forth between pages without having to load another page. 单击这些功能之一将加载该div并隐藏当前的div-这样,您就可以在页面之间来回切换而不必加载另一页面。 I changed it because loading times were too long. 我更改了它,因为加载时间太长。

However, now I want to sort of change it back. 但是,现在我想把它改回来。 I would like to result to be like this: 我想得到这样的结果:

A + B + C | A + B + C | D d

However, when on page D, if I click link B, div A will load (because its the default div on a separate page) 但是,在D页上时,如果我单击链接B,则会加载div A(因为其默认div在单独的页面上)

Is there a way to influence which div loads when a link is clicked? 单击链接时,有什么方法可以影响加载哪个div?

I see you've got jQuery in your page, so I'll use that to simplify things. 我看到您的页面中包含jQuery,因此我将使用它来简化事情。 You would add a click handler to your link to show its corresponding div and hide the others. 您可以在链接中添加一个点击处理程序,以显示其相应的div并隐藏其他div。 Give your links and divs ids and give your divs a common class: 给出链接和div ID,并给div一个普通的类:

$("#linkId").click(function() {
    $(".tabContent").hide();
    $("#tabContentId").show();
    return false;
});

Edit: To set click handlers on all the links at once, you could specify the id of its corresponding div as a custom attribute in the link. 编辑:要一次在所有链接上设置单击处理程序,可以将其对应的div的ID指定为链接中的自定义属性。 Then bind all the links that have that custom attribute set: 然后绑定所有设置了该自定义属性的链接:

$("a[contentId]").click(function() {
    $(".content").hide();
    $("#" + this.getAttribute("contentId")).show();
    return false;
});

Working demo: http://jsfiddle.net/gilly3/fyRA4/ 工作演示: http : //jsfiddle.net/gilly3/fyRA4/

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

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