gpt4 book ai didi

javascript - 如何在 dropmenu 中引用具有 id 的元素

转载 作者:行者123 更新时间:2023-11-28 02:50:07 25 4
gpt4 key购买 nike

请帮我写这个脚本。我试图将一些 php 代码链接到下拉菜单,但我没有任何反应....

代码是:

js代码:

function loadDoc1() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("show2").innerHTML = this.responseText;
}
};
xhttp.open("GET","user_manag.php",true);
xhttp.send();
}

$(document).ready(function() {
$("#a2").click(function() {
$("#show2")".load("user_manag.php",function(responseTxt, statusTxt, xhr) {
if(statusTxt == "success")
alert("External content loaded successfully!");
if(statusTxt == "error")
alert("Error: " + xhr.status + ": " + xhr.statusText);
});
});
});

HTML 标记:

<nav class="navbar navbar-inverse" style="border:4px solid white">
<div class="container-fluid">
<div class="navbar-header" style="margin-top:5px;">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a href="http://www.april-romania.ro"><img src="logo1.jpg" style="width:40px;height:40px;"></a>
</div>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav" >
<li><a href="#"><span class="glyphicon glyphicon-search"></span>Search</a></li>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">ADMIN
<span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a id="a1" href="#" action="loadDoc1()">User management</a></li>
<li><button id="button1">User management</button></li>
<li><a id="a2" href="#">Log report</a></li>
<li><a href="#">Change caseID</a></li>
<li><a href="#">Code tables</a></li>
</ul>
</li>
</div>
<div class="container-fluid text-center">
<div class="row content">
<div class="col-sm-1 sidenav" style="border: 4px solid white";>

</div>

<div id="show2" class="col-sm-8 text-left" style="background-color:rgb(200,200,200);border: 4px solid white; height: 100%">
hereeeeeeeeee

</div>
<div class="col-sm-3 sidenav" style="background-color:gray;border: 4px solid white">

</div>
</div>
</div>

user_manag.php 是:

<?php

echo "user management";

?>

我正在尝试更改 <div>#swow2 中的内容当我访问其中一个菜单链接时....我尝试使用 ajax、js、jQuery、属性操作,onclick ,链接,按钮....似乎没有任何效果。我认为它无法识别我为菜单中的“a”或“按钮”元素提供的 ID。

最佳答案

问题是您的 HTML 有几个缺少结束标记。正如评论中指出的那样,您在 JS 中也有错误。这是干净的脚本(现在可以使用):

HTML 和 JS

<nav class="navbar navbar-inverse" style="border:4px solid white">
<div class="container-fluid">
<div class="navbar-header" style="margin-top:5px;">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a href="http://www.april-romania.ro"><img src="logo1.jpg" style="width:40px;height:40px;"></a>
</div>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav" >
<li><a href="#"><span class="glyphicon glyphicon-search"></span>Search</a></li>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">ADMIN<span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a id="a1" href="#" action="loadDoc1()">User management</a></li>
<li><button id="button1">User management</button></li>
<li><a id="a2" href="#">Log report</a></li>
<li><a href="#">Change caseID</a></li>
<li><a href="#">Code tables</a></li>
</ul>
</li>
</ul>
</div>
</div>
</nav>
<div class="container-fluid text-center">
<div class="row content">
<div class="col-sm-1 sidenav" style="border: 4px solid white"></div>
<div id="show2" class="col-sm-8 text-left" style="background-color:rgb(200,200,200);border: 4px solid white; height: 100%">
hereeeeeeeeee
</div>
<div class="col-sm-3 sidenav" style="background-color:gray;border: 4px solid white"></div>
</div>
</div>

<script>
function loadDoc1() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("show2").innerHTML = this.responseText;
}
};
xhttp.open("GET","user_manag.php",true);
xhttp.send();
}

$(document).ready(function() {
$("#a2").click(function() {
$("#show2").load("user_manag.php",function(responseTxt, statusTxt, xhr) {
if(statusTxt == "success")
alert("External content loaded successfully!");
if(statusTxt == "error")
alert("Error: " + xhr.status + ": " + xhr.statusText);
});
});
});
</script>

关于javascript - 如何在 dropmenu 中引用具有 id 的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39704569/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com