简体   繁体   English

使用innerHTML / insertAdjacentHTML和JS替换Div内容?

[英]Replacing Div Content Using innerHTML/insertAdjacentHTML and JS?

I have a couple of sections in a website I am making and I have additional HTML which I would like to swap out the initial content for once a button has been pressed. 我在我正在制作的网站上有几个部分,我还有其他HTML,我想在按下按钮后更换初始内容。

I have done this kind of thing using jQuery and innerHTML/insertAdjacentHTML in tutorials before, but I can't quite grasp how to implement it into my own code. 我以前在教程中使用jQuery和innerHTML / insertAdjacentHTML做过这种事情,但我不太清楚如何将它实现到我自己的代码中。 I understand how it works but JS is not my strong point and I am trying to learn a bit more about it by tackling these kinds of thing. 我理解它是如何工作的,但JS不是我的强项,我试图通过解决这些问题来学习更多。

<section class="section-web new-section__white section new_section" id="web">

        <div class="row web-btns">
            <div class="col span-2-of-2 ">

                <a class="btn btn-ghost" href="#" target="_blank">Button 1</a>

                <a class="btn btn-ghost" href="#" target="_blank">Button 2</a>

                <a class="btn btn-ghost" href="#" target="_blank">Button 3</a>

                <a class="btn btn-ghost" href="#" target="_blank">Button 4</a>
            </div>
        </div>

 <!-- section (initial) button 1 -->
        <div class="row" id="button-1">
            <div class="col span-1-of-2">
                <h2>Text Button-1</h2>
                <p>Some Text</p>

        </div>

  <!-- section button 2 -->

  <!--
        <div class="row" id="button-2">
            <div class="col span-2-of-3">
               <h2>text Button-2</h2>
                <p>text</p>


            </div>
        </div>
-->

 <!-- section button 3 -->

        <!--
        <div class="row" id="button-3">
            <div class="col span-2-of-2">
                <h2>text Button-3</h2>
                <p>text</p>
            </div>
            </div
-->

 <!-- section button 4 -->

 <!--
    <div class="row" id="button-4">
            <div class="col span-2-of-2">
                <h2>text Button-4</h2>
                <p>text</p>
            </div>

        </div>
-->

    </section>

CSS: CSS:

* {
    margin: 0;
    padding: 0;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
}

html,
body {
    color: #555;
    font-family: 'Lato', sans-serif;
    font-weight: 300;
    font-size: 7px;
    text-rendering: optimizeLegibility;
    overflow-x: hidden;
}

h2 {
    font-size: 180%;
    word-spacing: 2px;
    margin: 15px;
    letter-spacing: 1px;
  text-align: center;
}

p {
    font-weight: 300;
    font-size: 15px;
    text-align: center;
    line-height: 1.5;
}

a {
    font-size: 15px;
    text-transform: uppercase;
}

.new-section__white {
      padding: 5%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    flex-wrap: wrap;
    position: relative;
    background-color: #fff;
    height: 100vh;
    width: 100vw;
}

.btn:link,
.btn:visited {
    display: inline-block;
    padding: 10px 20px;
    text-decoration: none;
    border-radius: 200px;
    -webkit-transition: background-color 0.2s, border 0.2s, color 0.2s;
    -moz-transition: background-color 0.2s, border 0.2s, color 0.2s;
    -o-transition: background-color 0.2s, border 0.2s, color 0.2s;
    transition: background-color 0.2s, border 0.2s, color 0.2s;
}

.btn-ghost:link,
.btn-ghost:visited {
    border: 1px solid #000;
    color: #000;
}

.btn-ghost:hover,
.btn-ghost:active {
    box-shadow: 2px 2px 10px #555;
    border: none;
    background-color: #000;
    color: #fff;
}

Here is a really basic CodePen. 这是一个非常基本的CodePen。 I have included the idea I have regarding how to achieve this in the JS field. 我已经包含了关于如何在JS领域实现这一点的想法。

https://codepen.io/caitlinmooneyx/pen/XQpMNe https://codepen.io/caitlinmooneyx/pen/XQpMNe

I know that I can use innerHTML or even insertAdjacentHTML using a constant, but again, unsure looking at existing code I have in another project how to implement into what I have written out. 我知道我可以使用innerHTML甚至insertAdjacentHTML使用常量,但再次,不确定查看我在另一个项目中的现有代码如何实现我写出来的内容。 Mostly I can't understand how to call the function into view (excuse my terminology). 大多数情况下,我无法理解如何将函数调用到视图中(请原谅我的术语)。

What is the best way to achieve this? 实现这一目标的最佳方法是什么?

If it is a button that interact with user, you should use button tag not anchor tag. 如果它是与用户交互的按钮,则应使用按钮标记而不是锚标记。 Here is a code that works based on your example: 这是一个基于您的示例工作的代码:

// button with onclick handler that calls a changeView function.

<div class="row web-btns">
  <div class="col span-2-of-2 ">
    <button onclick="changeView()" type="button" class="btn btn-ghost" href="#" target="_blank">Button 1</button>
  </div>
</div>

// identifier for paragraph tag

<div class="row" id="button-1">
  <div class="col span-1-of-2">
    <h2>Text Button-1</h2>
    <p id="some-text">Some Text</p>
  </div>
</div>

// changeView function implementation

const changeView = (view) => {
  const markup = 'asd';
  const p = document.getElementById('some-text');
  p.innerHTML = markup;
};

Right now markup variable is hardcoded as 'asd' but you can easily call changeView function with different 'view' for different button. 现在,标记变量被硬编码为“asd”,但您可以轻松地为不同的按钮调用具有不同“视图”的changeView函数。

You could approach this in a few ways, but the simplest is just to use innerHTML and switch out the content. 您可以通过几种方式来解决这个问题,但最简单的方法就是使用innerHTML并切换内容。

The code below isn't the simplest way to do this, but it does mean that you can break things down depending on how you want to switch up the content. 下面的代码不是最简单的方法,但它确实意味着你可以根据你想要切换内容的方式来分解。

 const markups = { intro: `<h1>This is the intro</h1>`, summary: `<p>This is the summary</p>`, }; const getMarkupSwitcher = markupData => element => markupToUse => element.innerHTML = markupData[markupToUse] const markupSwitcher = getMarkupSwitcher(markups) const switchMainDiv = markupSwitcher(document.getElementById('main')) switchMainDiv('intro') 
 <button onclick="switchMainDiv('intro')">Intro</button> <button onclick="switchMainDiv('summary')">Summary</button> <div id="main"></div> 

You can simply do: 你可以简单地做:

$( document ).ready(function() {
  $('.btn').on('click', (event) => {
    $('.content-section').hide();
    let section = $(event.target).data('section');
    $(`.section-${section}`).show();
  })
});

Since you seem to be using jQuery, i added a couple of properties to your HTML elements like data-section='2' to link the content to each button, you can also use libraries like Bootstrap that will generate this kind of menus for you. 既然你似乎在使用jQuery,我在你的HTML元素中添加了几个属性,比如data-section='2' ,将内容链接到每个按钮,你也可以使用像Bootstrap这样的库来为你生成这种菜单。

Here is a fiddle with a simple solution: JSFiddle 这是一个简单的解决方案: JSFiddle

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

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