gpt4 book ai didi

javascript - 为什么更改 元素的 href 在报价机上不起作用

转载 作者:行者123 更新时间:2023-12-03 03:01:14 27 4
gpt4 key购买 nike

我使用 .getJSON 方法来获取 data.json 文件,当我单击“新报价”按钮时,它运行良好。但是,Twitter和Tumblr不起作用,一开始我将它们的href设置为#,之后当我单击它们时,它们的href将更改为相应的。但是每次我单击twitter和Tumblr时,它们都不会去到正确的页面,他们的 URL 始终显示“http://localhost/freecodecamp.html#” '。哪里错了?我怎样才能实现这个功能呢?预先感谢!
p.s.my data.json 文件如下所示:
{"quote":[在那里放置引用], "author":[在他们的作者中], "backgroundC":[在那里放置颜色] }

var i = 0;
var currentQuote = '';
var currentAuthor = '';
$(document).ready(function() {
$("#buttonQuote").on("click", function() { //when click the "new quote" button
$.getJSON("data.json", function(json) { //get data.json from server
var html = "";
while (i <= 6) { //loop the data inside the data.json file
html += "<p>" + json.quote[i] + "</p>";
html += "<p class='author'>" + json.author[i] + "</p>";
$("body").css("background-color", json.backgroundC[i]); //set the css
$(".box").css("color", json.backgroundC[i]);
$("#buttonQuote").css("color", json.backgroundC[i]);
$("a").css("color", json.backgroundC[i]);
$(".box").html(html); //change box div's innerhtml
currentQuote = json.quote[i];
currentAuthor = json.author[i];
i = (i == 6) ? 0 : i + 1;
break;
}
}); // the end of .getJSON
}); //the end of click the "new queto"
$("#a-twitter").on('click', function() { /*when click twitter, its href attribute will change to the corresponding one, this code I copy from the freeCodeCamp example.*/
$("#a-twitter").attr('href', 'https://twitter.com/intent/tweet?hashtags=quotes&related=freecodecamp&text=' + encodeURIComponent('"' + currentQuote + '" ' + currentAuthor));
});
$("#a-tumblr").on('click', function() { //when click tumblr, its href attribute will change to the corresponding one
$("#a-tumblr").attr('href', 'https://www.tumblr.com/widgets/share/tool?posttype=quote&tags=quotes,freecodecamp&caption=' + encodeURIComponent(currentAuthor) + '&content=' + encodeURIComponent(currentQuote) + '&canonicalUrl=https%3A%2F%2Fwww.tumblr.com%2Fbuttons&shareSource=tumblr_share_button');
});
});
body {
background-color: #e66b19;
height: 100%;
}

.box {
/*set the quote area css*/
text-indent: 30px;
margin: auto;
font-size: 24px;
font-weight: bold;
width: 500px;
height: 250px;
background-color: #ebebe0;
color: #e66b19;
padding: 5px;
}

.button-box {
/*set the link and button area css*/
margin: 10px auto;
width: 500px;
height: 50px;
}

a {
/*set <a> element applying on twitter and tumblr*/
display: inline-block;
text-align: center;
height: 40px;
width: 40px;
margin: 0px 10px;
color: #e66b19;
background-color: #ebebe0;
cursor: pointer;
}

#buttonQuote {
/*set the "mew quote" button*/
background-color: #ebebe0;
padding: 5px 20px;
font-size: 24px;
font-weight: bold;
color: #e66b19;
float: right;
}

.author {
float: right;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Quote Machine</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>

<body>
<div class="box">Click New Quote Button</div>
<div class="button-box">
<a target="_blank" id="#a-twitter" href="#"><i class="fa fa-twitter" style="font-size:36px;"></i></a>
<a target="_blank" id="#a-tumblr" href="#"><i class="fa fa-tumblr" style="font-size:36px;"></i></a>
<button id="buttonQuote">New quote</button>
</div>
</body>

</html>

最佳答案

您正在输入 #登录 id

 <a target="_blank" id="#a-twitter" href="#">
<a target="_blank" id="#a-tumblr" href="#">

并在 $(#a-tumblr) 上获取事件和$(#a-twitter)

只需删除 #来自ids您的a标签,这将起作用

喜欢

  <a target="_blank" id="a-twitter" href="#">
<a target="_blank" id="a-tumblr" href="#">

关于javascript - 为什么更改 <a> 元素的 href 在报价机上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47375797/

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