Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery在同一页的另一个DIV中加载DIV_Jquery - Fatal编程技术网

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

Jquery在同一页的另一个DIV中加载DIV,jquery,Jquery,HTML: Jquery: .first{ display:none} .second{ display:none} 我想调用并加载“info_ly”DIV中的“rel”DIV 使用这个Jquery代码,我只得到“info_ly”DIV中的文本“first”或“second” 如何在“info_ly”DIV中加载类为“first”的DIV或类为“second”的DIV 试试这个: $(".someclass").click(function() { $(".info_ly").html($

HTML:

Jquery:

.first{ display:none}
.second{ display:none}
我想调用并加载“info_ly”DIV中的“rel”DIV

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

如何在“info_ly”DIV中加载类为“first”的DIV或类为“second”的DIV

试试这个:

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

$(".someclass").click(function() {  
    $(".info_ly").html($(this).html());
});
但是,如果其他div有id,它会变得更简单,例如
rel=“#first”
,或者使用
href=“#first”
使可点击元素锚定会更好


下面是我提到的使用锚的替代方法的一个示例:

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

这还有一个额外的好处,就是在未启用JS时可以优雅地降级(如果只添加一点代码,还可以添加书签)。

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

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

谢谢你,尼克。如果我使用这个HTML代码的问题是:文本1文本2文本3这里是一些文本第一部分第二部分​ 我不能用rel“info\u ly”来调用“info\u ly”div。我该怎么做?@Sergio-我不明白……那会把
info\u ly
中的内容放回那里,没有效果,你能更好地解释一下你想要什么吗?@Nick-这应该是一个菜单。如果有人点击“文本3”DIV,它将打开加载页面时的DIV。在本例中,如果单击它,它不会打开。你可以在:@Sergio-它确实加载了,但正如我说的,你正在将相同的内容加载到同一个东西中,没有效果,明白我在这里的意思了:@Nick-是的,我现在明白了。很抱歉我想做的事情是加载内容“这里是一些文本”再次如果“文本3”被点击。我该怎么做?把它放在另一个舱里?
<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>​
$(".someclass").click(function() {
  $(".info_ly").html($(this.hash).html());
});​
 $(".someclass").click(function() {  
       $('div#replacediv').replaceWith($('.second'));
    });