跳转传值:
1、location.href =xxx.html?s=${value}
2、<a href="xxx.html?s=${value}"> </a>
3、事件传值:
取值:
在location里找值:location.search.split(“=”)[1]
decodeURIComponent():如果接到的是中文需要转码成中文
eg:
传值music.html:
<body><input type="text" id="input"><script>var input = document.getElementById("input");input.onkeyup = function(event){if(event.keyCode==13){console.log(this.value);location.href = `search.html?s=${this.value}`}}</script></body>
接收值search.html:
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
<body>
<p id="p">搜索</p>
<script>
console.log(location.search.split("=")[1]); //
var keyword = location.search.split("=")[1];
var c = decodeURIComponent(keyword);
console.log(c); //中文
$("p").empty();
$("p").append(c);
</script>
</body>
eg:a标签跳转
<body>
<div class="content">
<div class="img">
<img src="" alt="">
</div>
<p class="name">我们终于放下了</p>
<p class="actor">许亚童</p>
</div> -->
</div>
<script>
var url = "http://192.168.4.18:3000/top/album";
ajax({
url:url,
method:"get",
success:res=>{
// var arr = res.albums;
var arr = res.playlists;
arr.forEach(item=>{
var {name,creator,coverImgUrl} =item
var actors = creator.nickname;
var famplate = `
<a href="html/detail.html?id=${item.id}">
<div class="music">
<div class="img">
<img class="image" src=${coverImgUrl} alt="">
</div>
<p class="name">${name}</p>
<p class="actor">${actors}</p>
</div>
</a>
`
$(".content").append(famplate);
})
}
})
</script>
</body>
eg:事件传值
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
<body>
<div class="item" data-aid="1234">
hello world1
</div>
<div class="item" data-aid="1235">
hello world2
</div>
<script>
$(".item").click(function(event){
console.log(event.target.dataset.aid);
})
</script>
</body>
