简体   繁体   English

.replaceWith()替换div中不同链接元素的内容

[英].replaceWith() to replace content in a div for different link elements

I am trying to load a div with different content based on the link I click... 我正在尝试根据点击的链接加载具有不同内容的div ...

While it seems to work for the first link when I click it, clicking the other links only replaces the content with the same content for 'encodeMe' , yet I have specified different content that I want to replace for 'htmlize-me' 当我单击它似乎对第一个链接有效时,单击其他链接仅将具有相同内容的内容替换为“ encodeMe”,但我指定了要替换为“ htmlize-me”的其他内容

The first run-through of this I did not use jQuery's .bind() function. 首先,我没有使用jQuery的.bind()函数。 I simply used .click() , and both had the same result. 我只是使用.click(),但两者的结果相同。 Looking through the jQuery API I thought using the .bind() function would bind each function within it to that particular page element, but it seems to apply it to all my links. 通过浏览jQuery API,我认为使用.bind()函数会将其中的每个函数绑定到该特定页面元素,但似乎将其应用于我的所有链接。

I've achieved the same effect using .hide and .show to toggle divs but I want to be more elegant about how I do that, and this was my attempted alternative... 我已经使用.hide和.show切换div达到了相同的效果,但是我想更加优雅地做到这一点,这是我尝试的替代方法...

here's the relevant html: 这是相关的html:

<label for="list-root">App Hardening</label>
<input type="checkbox" id="list-root" />
<ol>
   <li id="encode-me"><a class="show-popup" href="#">encodeMe()</a></li>
   <li id="htmlize-me"><a class="show-popup" href="#">htmlizeMe()</a></li>
</ol>
<div class="overlay-bg">
<div class="overlay-content">
    <div class="the-content"></div>
        <br><button class="close-button">Close</button>
    </div>
</div>

here's the script I made to trigger the content change: 这是我触发内容更改的脚本:

$('#encode-me').bind('click' , function() {
   $('div.the-content').replaceWith('<h3 style="color: #008ccc;"> function encodeMe( string ) </h3>' +
        'Found in <p>[web root]/redacted/redacted.asp</p>');
    }); 
});
$('#htmlize-me').bind('click' , function() {
   $('div.the-content').replaceWith('Hi, Im something different');
    }); 
});

Try something like this: 尝试这样的事情:

Use html() instead of replaceWith() 使用html()代替replaceWith()

 $('#encode-me').bind('click' , function() {
       $('div.the-content').html('<h3 style="color: #008ccc;"> function encodeMe( string ) </h3>' +
            'Found in <p>[web root]/redacted/redacted.asp</p>');
        }); 
    });
    $('#htmlize-me').bind('click' , function() {
       $('div.the-content').html("Hi, I'm something different");
        }); 
    });

replaceWith does exactly what it sounds like, it replaces the div with the h3, so when you click the second link there is no div. replaceWith功能完全一样,它将div替换为h3,因此,当您单击第二个链接时,没有div。

Try setting the innerHTML instead 尝试改为设置innerHTML

$('#encode-me').on('click' , function() {
   $('div.the-content').html('<h3 style="color: #008ccc;"> function encodeMe( string ) </h3>Found in <p>[web root]/redacted/redacted.asp</p>');
});
$('#htmlize-me').on('click' , function() {
   $('div.the-content').html('Hi, I\'m something different');
});

So I figured out a more clever way to do this! 因此,我想出了一种更聪明的方法! Use the DOM to your advantage - set up a nested list structure and change the content using .find() on parent and child elements the list. 利用DOM的优势-设置嵌套列表结构,并在列表的父元素和子元素上使用.find()更改内容。

Markup 标记

<span style="font-size:1.4em">Type  
    <ul class="row">
        <li><a href="#" class="show-popup">Blah</a>
            <div class="overlay-content">
                <p><a href="#" class="close"></a></p>
                    <p class="changeText">Blah</p>
            </div>
        </li>
        <li><a href="#" class="show-popup">Blah2</a>
            <div class="overlay-content">
                <p><a href="#" class="close"></a></p>
                    <p class="changeText">Blah2</p>
            </div>
        </li>
    </ul>
</span><br>
<!-- OVERLAYS -->
<div class="overlay"></div>

CSS CSS

.close {
    border-radius: 10px;
    background-image: url(../img/close-overlay.png);
    position: absolute;
    right:-10px;
    top:-15px;
    z-index:1002;
    height: 35px;
    width: 35px;
}

.overlay {
    position:absolute;
    top:0;
    left:0;
    z-index:10;
    height:100%;
    width:100%;
    background:#000;
    filter:alpha(opacity=60);
    -moz-opacity:.60;
    opacity:.60;
    display:none;
}
.overlay-content {
    position:fixed!important;
    width: 60%;
    height: 80%;
    top: 50%;
    left: 50%;
    background-color: #f5f5f5;
    display:none;
    z-index:1002;
    padding: 10px;
    margin: 0 0 0 -20%;
    cursor: default;
    border-radius: 4px;
    box-shadow: 0 0 5px rgba(0,0,0,0.9);
}

Script 脚本

$(document).ready(function(){
$('.show-popup').click(function() {
    var ce = this;
    $('.overlay').show('slow', function() {
        $(ce).parent().find('.overlay-content').fadeIn('slow');
    });
});
// show popup when you click on the link
$('.show-popup').click(function(event){
    event.preventDefault(); // disable normal link function so that it doesn't refresh the page
    var docHeight = $(document).height(); //grab the height of the page
    var scrollTop = $(window).scrollTop(); //grab the px value from the top of the page to where you're scrolling       
    $('.overlay').show().css({'height' : docHeight}); //display your popup and set height to the page height
    $('.overlay-content').css({'top': scrollTop+100+'px'}); //set the content 100px from the window top   
});
/*
// hides the popup if user clicks anywhere outside the container
$('.overlay').click(function(){
    $('.overlay').hide();
})
*/
// prevents the overlay from closing if user clicks inside the popup overlay
$('.overlay-content').click(function(){
    return false;
});
$('.close').click(function() {
    $('.overlay-content').hide('slow', function() {
        $('.overlay').fadeOut();
    });
  });
});

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

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