0

好的,jQuery 的新手,所以希望这是一个简单的问题,我只是错过了一些东西。当单击以下链接时,我想将我网站上的一个页面上的 2 个 div 加载到另一个页面上的 div 中:

$('#result').load('test.html #div1 #div2');

这是我到目前为止所拥有的:

<a onmouseover="" style="cursor: pointer;" onclick='$("#col2").load("oldPage.htm #div1");'>Display content</a>

这有效,但它只加载 1 个 Div,如果我尝试从“oldPage”添加另一个,它不起作用。想法?再次记住我是新手。

谢谢。

4

1 回答 1

1

Let's use a function to make this easier. I'll call it load_divs:

HTML:

<a style="cursor: pointer;" onclick='load_divs();return false;'>Display content</a>

JavaScript:

function load_divs() {
    $("#col2").load("oldPage.htm #div1");
    $("#col2").append($("<div>").load("oldPage.htm #div2"));
}

An even better way to attach this event handler would be to give your a element an id and add the handler using jQuery:

HTML:

<a id="display_link" style="cursor: pointer;">Display content</a>

JS:

$("#display_link").click(function() {
    $("#col2").load("oldPage.htm #div1");
    $("#col2").append($("<div>").load("oldPage.htm #div2"));
});

Alternatively, if the divs you want to load are side-by-side in the DOM, you can wrap them in another div then load just that one.

于 2013-08-05T17:47:58.683 回答