Jquery在同一页面的另一个DIV中加载DIV

时间:2010-06-11 11:18:35

标签: jquery

HTML:

<div class="someclass" rel="first">text 1</div>
<div class="someclass" rel="second">text 2</div></div></div>
<div class="info_ly">here is some text</div>
<div class="first" > the first DIV </div>
<div class="second" > the second DIV </div>

CSS:

.first{ display:none}
.second{ display:none}

Jquery的:

$(".someclass").click(function() {  
$(".info_ly").html($(this).attr('rel'));
});

我想在“info_ly”DIV中调用并加载“rel”DIV。

使用这个Jquery代码,我在“info_ly”DIV中只获得文本“first”或“second”。

如何在“info_ly”DIV中使用“first”类或DIV类加载“第二”类的DIV?

3 个答案:

答案 0 :(得分:7)

像这样:

$(".someclass").click(function() {  
  $(".info_ly").html($("." + $(this).attr('rel')).html());
});

You can view a demo here,如果其他div有ID,则会更简单,例如<{1}},或者使rel="#first"的可点击元素锚点更好。


以下是我使用锚点提到的替代方法的示例:

href="#first"

你的jQuery缩小到这个:

<a class="someclass" href="#first">text 1</a>
<a class="someclass" href="#second">text 2</a>
<div class="info_ly">here is some text</div>
<div id="first" class="contentDiv"> the first DIV </div>
<div id="second" class="contentDiv"> the second DIV </div>​

如果没有启用JS(以及只添加一些代码时可以添加书签),you can see a demo of this approach here可以优雅地降低这种优势。

答案 1 :(得分:0)

试试这个:

$(".someclass").click(function() {  
    $(".info_ly").html($(this).html());
});

答案 2 :(得分:0)

创建一个id = replacementiv的div,您要在其中添加新数据并加载具有第二类

的新数据
 $(".someclass").click(function() {  
       $('div#replacediv').replaceWith($('.second'));
    });