Ändern Sie Inhalte in einem anderen Div mit Javascript
P粉769045426
P粉769045426 2023-09-05 23:33:53
0
1
556
<p>我希望通过单击链接容器并使用 JavaScript-Code (<code>show-text()</code>) 来更改另一个 div (<code>id="new-text"< /code>) 的内容代码>)</p> <p> <pre class="brush:html;toolbar:false;"> <Abschnitt> <div class="row"> <div class="col-xs-12 col-sm-12 col-md-12 col-lg-3 col-xl-3"> <a href="#" id="text-1" onclick="show-text()"> <div class="home-side-menu">Leitbild</div></a> <a href="#" id="text-2" onclick="show-text()"> <div class="home-side-menu">Briefschreiben</div></a> <a href="#" id="text-3" onclick="show-text()"> <div class="home-side-menu">Friedensideen</div></a> <a href="#" id="text-4" onclick="show-text()"> <div class="home-side-menu">Power Of Love</div></a> </div> </div> <div class="col-xs-12 col-sm-12 col-md-12 col-lg-9 col-xl-9"> <div class="text-formatting" id="new-text"> </div> </div> </div> </Abschnitt> <script type="text/javascript"> Funktion show-text(id){ if id="text-1" document.getElementById('new-text').innerHTML="BEISPIELTEXT 1" if id="text-2" document.getElementById('new-text').innerHTML="BEISPIELTEXT 2" if id="text-3" document.getElementById('new-text').innerHTML="BEISPIELTEXT 3" if id="text-4" document.getElementById('new-text').innerHTML="BEISPIELTEXT 4" } </script> </pre> </p>
P粉769045426
P粉769045426

Antworte allen(1)
P粉232793765


更改后的代码(工作)

<section>
    <div class="row">
        <div class="col-xs-12 col-sm-12 col-md-12 col-lg-3 col-xl-3">
            <a href="#" id="text-1" onclick="showText('text-1')">
                <div class="home-side-menu">Mission Statement</div>
            </a>
            <a href="#" id="text-2" onclick="showText('text-2')">
                <div class="home-side-menu">Letter-writing</div>
            </a>
            <a href="#" id="text-3" onclick="showText('text-3')">
                <div class="home-side-menu">Peace Ideas</div>
            </a>
            <a href="#" id="text-4" onclick="showText('text-4')">
                <div class="home-side-menu">Power Of Love</div>
            </a>
        </div>
        <div class="col-xs-12 col-sm-12 col-md-12 col-lg-9 col-xl-9">
            <div class="text-formatting" id="new-text">
            </div>
        </div>
    </div>
</section>

<script type="text/javascript">
    function showText(id) {
        if (id === "text-1")
            document.getElementById('new-text').innerHTML = "SAMPLE TEXT 1";
        if (id === "text-2")
            document.getElementById('new-text').innerHTML = "SAMPLE TEXT 2";
        if (id === "text-3")
            document.getElementById('new-text').innerHTML = "SAMPLE TEXT 3";
        if (id === "text-4")
            document.getElementById('new-text').innerHTML = "SAMPLE TEXT 4";
    }
</script>

onclick="show-text() 内,传递有效参数(与您的 IF 条件匹配),例如“text-1”或“text-2”..

应该是
其余部分也是如此。

并在 Script 中使用 === (严格相等检查)进行比较。检查Js 比较运算符

Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage