以下代码里,怎么样用JS增添一个删除键,删除购物车内对应的商品? 55
<!doctypehtml><htmllang="en"><head><metacharset="UTF-8"><title>Document</title><style...
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
*{margin:0;padding: 0;}
#div_id p{width:80px;height:30px;text-align: left;border: 1px solid #000;line-height: 30px;}
#div_id p button{float:right;height:30px;}
</style>
<script src = "js/tool.js"></script>
<script>
window.onload = function(){
var button = document.getElementsByTagName("button")
for(var i = 0; i < button.length - 1 ;i++){
button[i].onclick = function(){
alert("加入购物车成功")
var div = document.getElementById("div_id")
var p = createNodeWithText("p",this.innerHTML);
div.appendChild(p);
/*var del = createNodeWithText("button","×")
p.appendChild(del);
del.onclick = function(){
div.removeChild(p)
} */
setCookie("购物",div.innerHTML,getDate(100))
}
}
var isTrue = false;
var button1 = document.getElementById("button_id")
button1.onclick = function(){
if(isTrue == false){
button1.innerHTML ="隐藏购物车"
isTrue = true;
var div = document.getElementById("div_id");
if(getCookie("购物") != undefined){
div.innerHTML = getCookie("购物");}
div.style.display = "block";
}else if(isTrue){
button1.innerHTML ="显示购物车"
isTrue = false;
var div = document.getElementById("div_id");
if(getCookie("购物") != undefined){
div.innerHTML = getCookie("购物");
div.style.display = "none";
}
}
}
}
</script>
</head>
<body>
<button>商品1</button>
<button>商品2</button>
<button>商品3</button>
<button>商品4</button>
<button>商品5</button><br/><br/>
<button id = "button_id">显示购物车</button>
<div id = "div_id" style="display:none;"></div>
</body>
</html> 展开
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
*{margin:0;padding: 0;}
#div_id p{width:80px;height:30px;text-align: left;border: 1px solid #000;line-height: 30px;}
#div_id p button{float:right;height:30px;}
</style>
<script src = "js/tool.js"></script>
<script>
window.onload = function(){
var button = document.getElementsByTagName("button")
for(var i = 0; i < button.length - 1 ;i++){
button[i].onclick = function(){
alert("加入购物车成功")
var div = document.getElementById("div_id")
var p = createNodeWithText("p",this.innerHTML);
div.appendChild(p);
/*var del = createNodeWithText("button","×")
p.appendChild(del);
del.onclick = function(){
div.removeChild(p)
} */
setCookie("购物",div.innerHTML,getDate(100))
}
}
var isTrue = false;
var button1 = document.getElementById("button_id")
button1.onclick = function(){
if(isTrue == false){
button1.innerHTML ="隐藏购物车"
isTrue = true;
var div = document.getElementById("div_id");
if(getCookie("购物") != undefined){
div.innerHTML = getCookie("购物");}
div.style.display = "block";
}else if(isTrue){
button1.innerHTML ="显示购物车"
isTrue = false;
var div = document.getElementById("div_id");
if(getCookie("购物") != undefined){
div.innerHTML = getCookie("购物");
div.style.display = "none";
}
}
}
}
</script>
</head>
<body>
<button>商品1</button>
<button>商品2</button>
<button>商品3</button>
<button>商品4</button>
<button>商品5</button><br/><br/>
<button id = "button_id">显示购物车</button>
<div id = "div_id" style="display:none;"></div>
</body>
</html> 展开
2个回答
推荐于2017-11-24
展开全部
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
*{margin:0;padding: 0;}
#div_id p{width:80px;height:30px;text-align: left;border: 1px solid #000;line-height: 30px;}
#div_id p button{float:right;height:30px;}
</style>
<script src = "js/tool.js"></script>
<script>
window.onload = function(){
var div = document.getElementById("div_id");
var button = document.getElementsByTagName("button")
for(var i = 0; i < button.length - 1 ;i++){
button[i].onclick = function(){
alert("加入购物车成功");
var p = document.createElement("p");
var pContent = document.createTextNode(this.innerHTML);
p.appendChild(pContent);
div.appendChild(p);
var delBtn = document.createElement("button");
var delBtnContent = document.createTextNode("x");
delBtn.appendChild(delBtnContent);
p.appendChild(delBtn);
delBtn.onclick = function(){
div.removeChild(p);
}
//setCookie("购物",div.innerHTML,getDate(100))
}
}
var isTrue = false;
var button1 = document.getElementById("button_id")
button1.onclick = function(){
if(isTrue == false){
button1.innerHTML ="隐藏购物车"
isTrue = true;
/*if(getCookie("购物") != undefined){
div.innerHTML = getCookie("购物");
}*/
div.style.display = "block";
}else if(isTrue){
button1.innerHTML ="显示购物车"
isTrue = false;
div.style.display = "none";
/*if(getCookie("购物") != undefined){
div.innerHTML = getCookie("购物");
div.style.display = "none";
}*/
}
}
}
</script>
</head>
<body>
<button>商品1</button>
<button>商品2</button>
<button>商品3</button>
<button>商品4</button>
<button>商品5</button><br/><br/>
<button id = "button_id">显示购物车</button>
<div id = "div_id" style="display:none;"></div>
</body>
</html>
追问
哥们需要保留过期时间...,刷新页面cookie也不会失效
追答
我只是按照你说的 增添一个删除键来做,其他的 cookie 这个,你上面一开始也没说要怎么做,具体也不是很清楚,所以只弄了个 删除的按钮,只是一个参考,不一定是你想要的效果。谢谢
展开全部
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
*{margin:0;padding: 0;}
#div_id p{width:80px;height:30px;text-align: left;border: 1px solid #000;line-height: 30px;}
#div_id p button{float:right;height:30px;}
</style>
<script src = "js/tool.js"></script>
<script>
window.onload = function(){
var button = document.getElementsByTagName("button")
for(var i = 0; i < button.length - 1 ;i++){
button[i].onclick = function(){
alert("加入购物车成功")
var div = document.getElementById("div_id")
var p = createNodeWithText("p",this.innerHTML);
div.appendChild(p);
/*var del = createNodeWithText("button","×")
p.appendChild(del);
del.onclick = function(){
div.removeChild(p)
} */
setCookie("购物",div.innerHTML,getDate(100))
}
}
var isTrue = false;
var button1 = document.getElementById("button_id")
button1.onclick = function(){
if(isTrue == false){
button1.innerHTML ="隐藏购物车"
isTrue = true;
var div = document.getElementById("div_id");
if(getCookie("购物") != undefined){
div.innerHTML = getCookie("购物");}
div.style.display = "block";
}else if(isTrue){
button1.innerHTML ="显示购物车"
isTrue = false;
var div = document.getElementById("div_id");
if(getCookie("购物") != undefined){
div.innerHTML = getCookie("购物");
div.style.display = "none";
}
}
}
}
function del(i){
var body = document.getElementsByTagName("body")[0];
var shop = document.getElementById("shop"+i);
var del =document.getElementById("del"+i);
body.removeChild(shop);
body.removeChild(del);
}
</script>
</head>
<body>
<button id="shop1">商品1</button><a href="#" onclick="del(1)" id="del1">x</a>
<button id="shop2">商品2</button><a href="#" onclick="del(2)" id="del2">x</a>
<button id="shop3">商品3</button><a href="#" onclick="del(3)" id="del3">x</a>
<button id="shop4">商品4</button><a href="#" onclick="del(4)" id="del4">x</a>
<button id="shop5">商品5</button><a href="#" onclick="del(5)" id="del5">x</a>
<br/><br/>
<button id = "button_id">显示购物车</button>
<div id = "div_id" style="display:none;"></div>
</body>
</html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
*{margin:0;padding: 0;}
#div_id p{width:80px;height:30px;text-align: left;border: 1px solid #000;line-height: 30px;}
#div_id p button{float:right;height:30px;}
</style>
<script src = "js/tool.js"></script>
<script>
window.onload = function(){
var button = document.getElementsByTagName("button")
for(var i = 0; i < button.length - 1 ;i++){
button[i].onclick = function(){
alert("加入购物车成功")
var div = document.getElementById("div_id")
var p = createNodeWithText("p",this.innerHTML);
div.appendChild(p);
/*var del = createNodeWithText("button","×")
p.appendChild(del);
del.onclick = function(){
div.removeChild(p)
} */
setCookie("购物",div.innerHTML,getDate(100))
}
}
var isTrue = false;
var button1 = document.getElementById("button_id")
button1.onclick = function(){
if(isTrue == false){
button1.innerHTML ="隐藏购物车"
isTrue = true;
var div = document.getElementById("div_id");
if(getCookie("购物") != undefined){
div.innerHTML = getCookie("购物");}
div.style.display = "block";
}else if(isTrue){
button1.innerHTML ="显示购物车"
isTrue = false;
var div = document.getElementById("div_id");
if(getCookie("购物") != undefined){
div.innerHTML = getCookie("购物");
div.style.display = "none";
}
}
}
}
function del(i){
var body = document.getElementsByTagName("body")[0];
var shop = document.getElementById("shop"+i);
var del =document.getElementById("del"+i);
body.removeChild(shop);
body.removeChild(del);
}
</script>
</head>
<body>
<button id="shop1">商品1</button><a href="#" onclick="del(1)" id="del1">x</a>
<button id="shop2">商品2</button><a href="#" onclick="del(2)" id="del2">x</a>
<button id="shop3">商品3</button><a href="#" onclick="del(3)" id="del3">x</a>
<button id="shop4">商品4</button><a href="#" onclick="del(4)" id="del4">x</a>
<button id="shop5">商品5</button><a href="#" onclick="del(5)" id="del5">x</a>
<br/><br/>
<button id = "button_id">显示购物车</button>
<div id = "div_id" style="display:none;"></div>
</body>
</html>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询