简体   繁体   English

jQuery replaceWith无法正确加载div

[英]jQuery replaceWith not loading div properly

I'm new to jQuery but am hoping to replace a <div> with another that contains a slideshow when I click on a link. 我是jQuery的新手,但我希望在单击链接时将<div>替换为包含幻灯片的另一个<div> The problem with my code however is that when I click the link, the <div> I want to load isn't showing. 但是,我的代码存在的问题是,当我单击链接时,要显示的<div>没有显示。 Instead I just see div.slidesContainer for example. 相反,我只看到例如div.slidesContainer

My HTML: 我的HTML:

<div id="leftText">
  <h2>Heading.</h2>
  <p>Text. <a href="/" id ="startSlide" %>'Learn more.'</a></p>
</div>
<div id="slide">
<div class="slides_container">
  <div>
    <h1>Slide heading.</h1>
      <p>Slide text.</p>
  </div>
</div>
</div>

My jQuery: 我的jQuery:

$(function(){
    $("a#startSlide").click(function(){
        $('div#leftText').replaceWith('div#slide');
        return false;
    });
});

Here's a jsFiddle if it'll help: http://jsfiddle.net/HR43b/ 如果有帮助,请使用jsFiddle: http : //jsfiddle.net/HR43b/

You need to pass an HTML or jQuery object to replaceWith: http://api.jquery.com/replaceWith/ 您需要传递HTML或jQuery对象来替换: http ://api.jquery.com/replaceWith/

.replaceWith( newContent ) .replaceWith(newContent)

newContentThe content to insert. newContent要插入的内容。 May be an HTML string, DOM element, or jQuery object. 可以是HTML字符串,DOM元素或jQuery对象。

.replaceWith( function ) .replaceWith(函数)

functionA function that returns content with which to replace the set of matched elements. function一个函数,该函数返回用来替换匹配元素集的内容。

String: 串:

$('div.inner').replaceWith('<h2>New heading</h2>');

Object: 宾语:

$('div.third').replaceWith($('.first'));

Callback: 打回来:

$('div.container').replaceWith(function() {
  return $(this).contents();
});

You have to pass a selector, not a string: 您必须传递选择器,而不是字符串:

$(function() {
    $("a#startSlide").click(function() {
        $('div#leftText').replaceWith($('#slide'));

        $('#slide').css('visibility', 'visible');
        return false;
    });
});

Demo: http://jsfiddle.net/HR43b/3/ 演示: http : //jsfiddle.net/HR43b/3/

Try this. 尝试这个。

$('div#leftText').replaceWith('<div id="slide"></div>');

Or 要么

$('div#leftText').replaceWith($('div#slide'));

Check this: 检查一下:

div#slide {
    visibility:hidden;
}

your <div> is hidden.. before replacing with other content change its behavior to visible. 您的<div>已隐藏。在替换为其他内容之前,请将其行为更改为可见。 and then use replaceWith 然后使用replaceWith

your altered jsfiddle 您更改的jsfiddle

 $(function(){
        $("a#startSlide").click(function(){
            $('div#slide').css('visibility','visible');
            $('div#leftText').replaceWith($('div#slide'));
            return false;
        });
    });

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

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