.replaceWith 无法使用 .click 函数加载另一个值
.replaceWith can't load another value using .click function
我有两个.on('click')
函数(#addBooksId,#addCategoryId(当我尝试单击文档对象模型中的一个函数时,它使用.replaceWith
替换新值,但是当我尝试单击文档对象模型中的另一个函数时,它不会替换新值。如何在不刷新浏览器的情况下执行此操作?而不是单击其中一个函数并再次刷新我的浏览器(重复(每次单击函数时我想做什么,它都会同时更改值。这是我下面的代码。
$(document).on("click","#addBooksId",function() {
$.get('addBooks.html', function(data)
{ //The current page
$('#form').replaceWith(data); // Receive with value
alert('This is add books section');
});
});
$(document).on("click","#addCategoryId",function() {
$.get('addCategory.html', function(data)
{ //The current page
$('#form').replaceWith(data); // Receive with value
alert('This is add category section');
});
});
主.html
< script type = "text/javascript"
src = "https://code.jquery.com/jquery-1.12.3.min.js" >
< /script>
$(document).on("click", "#addBooksId", function() {
$.get('addBooks.html', function(data) { / / The current page
$('#form').replaceWith(data); // Receive with value
alert('This is add books section');
});
});
$(document).on("click", "#addCategoryId", function() {
$.get('addCategory.html', function(data) { //The current page
$('#form').replaceWith(data); // Receive with value
alert('This is add category section');
});
});
body {
padding: 0;
margin: 0;
overflow-y: scroll;
font-family: Arial;
font-size: 15px;
}
#container {
background-color: #707070;
}
#menu {
width: 1250px;
margin: 0 auto;
text-align: left;
}
#menu ul {
list-style-type: none;
/*Remove bullets*/
padding: 0;
margin: 0;
position: relative;
}
#menu ul li {
display: inline-block;
}
#menu ul li:hover {
text-decoration: none;
color: black;
}
#menu ul li a,
visited {
color: #CCC;
/*Color of text*/
display: block;
/*Takes up the full width available*/
padding: 15px;
text-decoration: none;
}
#menu ul li a:hover {
color: #CCC;
text-decoration: none;
}
#menu ul li:hover ul {
display: block;
}
#menu ul ul li {
display: block;
}
#menu ul ul {
display: none;
position: absolute;
background-color: #707070;
min-width: 140px;
/*Width when hover*/
}
#menu ul ul li a:hover
/*Color of text when hover*/
{
color: #099;
}
<!doctype html>
<html>
<head>
<title>Main</title>
</head>
<body>
<div id="container">
<div id="menu">
<ul>
<li>
<a href="#">Manage Books</a>
<ul>
<div id="addBooksId">
<li><a href="#">Add Books</a>
</li>
</div>
<div id="addCategoryId">
<li><a href="#">Add Category</a>
</li>
</div>
</ul>
</li>
</ul>
</div>
<!--- end menu --->
</div>
<!--- end container --->
<div id="form">
<p>Hi</p>
</div>
</body>
</html>
添加书籍.html
.addBooks {
clear: both;
width: 800px;
margin: 0 auto;
margin-top: 10%;
}
.addBooks label {
float: left;
width: 150px;
text-align: right;
}
.addBooks input {
text-align: left;
margin-left: 100px;
}
<!doctype html>
<html>
<head>
</head>
<body>
<div id="container">
<div class="addBooks">
<h1>Add Books</h1>
<hr>
<form action="#">
<fieldset>
<div class="form-field">
<label for="bookId">Book Id:</label>
<input type="text" id="bookId" name="bookId">
</div>
<br />
<div class="form-field">
<label for="bookName">Book Name:</label>
<input type="text" id="bookName" name="bookName">
</div>
</fieldset>
</form>
</div>
<!--- end of addBooks --->
</div>
<!--- end of container --->
</body>
</html>
添加类别.html
.addCategory {
clear: both;
width: 800px;
margin: 0 auto;
margin-top: 10%;
}
.addCategory label {
float: left;
width: 150px;
text-align: right;
}
.addCategory input {
text-align: left;
margin-left: 100px;
}
<!doctype html>
<html>
<head></head>
<body>
<div id="container">
<div class="addCategory">
<h1>Add Category</h1>
<hr>
<form action="#">
<fieldset>
<label for="categoryName">Category:</label>
<input type="categoryName" id="categoryName" name="categoryName">
<br />
</fieldset>
</form>
</div>
<!--- end of addCategory --->
</div>
<!--- end of container --->
</body>
</html>
jQuery replaceWith(( 函数实际上替换了你通过选择器找到的节点,在你的例子中是 $("#form"(。
我想您要做的是替换节点的内部内容而不是整个 #form 节点,这可以通过以下方式完成:
$("#form").empty().append(data);
为什么需要注册点击两次。如果这解释了,请参阅此示例。
Note the following points though:
- 检查条件是否满足并执行
replaceWith
。 - 检查是否有任何
data
old data and new data
是否相同。 - 最后也是最重要的一点,检查所有
ids
是否存在。
EDITED ANSWER
替换代码中的以下部分,它可以完美运行:
$(document).on("click", "#addBooksId", function() {
$.get("addBooks.html", function(data) {
$( "#form" ).empty();
$('#form').append(data); // Receive with value
alert('This is add books section');
});
});
$(document).on("click", "#addCategoryId", function() {
$.get('addCategory.html', function(data) { //The current page
$( "#form" ).empty();
$('#form').append(data); // Receive with value
alert('This is add category section');
});
});
上述代码的问题在于,一旦第一次执行replaceWith
,例如您选择了添加框,form
id 将替换为 div id container
表示addBooks
,因此当您选择 addCatagory
时,没有要replaceWith
form id
,因为它已经替换为addBooks
因此数据不会被绑定。
所以$('#form').replaceWith(data);
替换为
$( "#form" ).empty();
$('#form').append(data);
或简单地:
$('#form').empty().append(data);
甚至更简单:
$('#form').html(data);
Note:
上面的代码只能在网络服务器上运行,而不是直接在浏览器上运行。
尝试将点击按钮替换为以下内容:
$(document).on("click","#foobar",function() {
// code here
});
在此处阅读何时使用click
或on click
- .replaceWith 无法使用 .click 函数加载另一个值
- 使通过函数加载的JSON可用于其他函数
- 加载 JS 函数加载时出现问题
- jQuery:使用on函数加载插件
- Javascript函数加载(通过引用?)
- Eclipse Phonegap无法使用jquery.load函数加载外部字符串文件
- 如何使用ffi将WinApi函数加载到Node.js中
- 触发函数加载替换模糊()
- 嵌套依赖主干.js具有 require.js 主干的视图.js导致视图作为对象而不是函数加载
- Angular JS ng-click在通过另一个javascript函数加载HTML时不起作用
- 我的jQuery函数加载了一个HTML,但该HTML不会从不同的函数(从flowplayer)加载不同的jQuery选择
- jQuery运行函数加载和键控
- 为什么我不能得到由 .load() 函数加载的 html
- 如何避免 Jquery 函数加载缓慢
- 使用自己的 JavaScript 函数加载地图图块
- 通过 loadURL 函数加载标记图层时更改地图框中的标记颜色
- 错误 JQuery 函数加载图像预览与 Safari 浏览器
- blogger.com 函数加载未捕获引用错误 $ 未定义
- jQuery - 在通过加载函数加载操作页面时在操作页面中设置字段值
- 延迟流星函数加载