所以我有一张Apple Music徽章。用户点击徽章后,我想要一个弹出窗口,询问用户是否要使用明确版本的歌曲或清洁版本,相应的按钮会执行操作。
目前我有一个javascript弹出窗口,应该允许用户按下" OK"要重定向到显式版本并按"取消"重定向到Clean版本。这个选项对我来说很好,但按钮没有按照我的预期去做,两个按钮都重定向到显式版本。
到目前为止,这是我的HTML和JavaScript ..
<a class="AppleMusic"
href="**explicit-link**"
style="display:inline-block;
overflow:hidden;
background:url(https://linkmaker.itunes.apple.com/assets/shared/badges/en-us/music-lrg.svg) no-repeat;
width:150px;
height:55px;
background-size:contain;">
</a>
<script type='text/javascript'>
$(window).on('load', function () {
$(".AppleMusic").on("click", function (event) {
if (confirm("This will redirect to the explict version of the song. Press 'Cancel' If you'd like to be redirected to the Clean version.")) {
return true;
}
else {
window.location = "**clean-link**";
}
});
});
</script>
问题是无论点击什么,它都会重定向到显式(geo.itunes.apple.com)网址。
如果可能的话,我不想使用UI对话框。
答案 0 :(得分:1)
尝试将您的功能更改为:
$(".AppleMusic").on("click", function(event){
event.preventDefault();
if (confirm("This will redirect to the explict version of the song. Press 'Cancel' If you'd like to be redirected to the Clean version.")){
window.location = "https://geo.itunes.apple.com/us/album/feelinme-feat-adrian-stresow/id1224174169?i=1224174173&mt=1&app=music&at=1l3vwYm&ct=FEELINME"
}
else {
window.location = "https://www.google.com/search?site=&q=clean+url";
}
});
防止默认将会阻止所点击对象的默认行为。
答案 1 :(得分:0)
$(function() {
$(".AppleMusic").on("click", function(event) {
event.preventDefault();
if (confirm("This will redirect to the explict version of the song. Press 'Cancel' If you'd like to be redirected to the Clean version.")){
var location = "https://geo.itunes.apple.com/us/album/feelinme-feat-adrian-stresow/id1224174169?i=1224174173&mt=1&app=music&at=1l3vwYm&ct=FEELINME";
window.open(location, "_self");
} else {
var location = "https://www.google.com/search?site=&q=clean+url";
window.open(location, "_self");
}
});
});
使用相同的原始网址