gpt4 book ai didi

javascript - 在JS中访问ready函数之外的变量值

转载 作者:行者123 更新时间:2023-11-28 14:46:23 25 4
gpt4 key购买 nike

为什么在下面的代码中,我无法获取变量x的值?这是代码-

var x;
$(document).ready(function(){
x=9;
});
console.log(x);

如何在 $(document).ready(function(){}); 之外访问变量“x”值?

$(document).ready(function(){
var arrQuotes=[], arrAuthor = [], colors=['#132145','#800000','#990404','#008080','#16a085','#AEBF4E','#27ae60','#ffc0cb', '#2c3e50', '#f39c12', '#e74c3c', '#9b59b6', '#FB6964', '#342224', "#472E32", "#BDBB99", "#77B1A9", "#73A857"], index;
$.getJSON(
"https://codepen.io/coder_at_work/pen/dVPxBm.js",
function(data) {
data.forEach(function(object){
arrQuotes.push(object["quote"]);
arrAuthor.push(object["author"]);
});
}
);

$("#quote").on("click",function(){
index = Math.floor(Math.random()*arrQuotes.length);
currentQuote = arrQuotes[index];
currentAuthor = arrAuthor[index];
$(".content").html("<span class='fa fa-quote-left'></span>"+arrQuotes[index]);
$(".author").html("<span>- </span>"+arrAuthor[index]);
var c= Math.floor(Math.random()*colors.length);
$("body").animate({
backgroundColor: colors[c],
color: colors[c]
},1500);
$(".button").animate({
backgroundColor: colors[c]
},1500);
$(".author").animate({
color: colors[c]
},1500);

});

$("#tweet-quote").attr('href', 'https://twitter.com/intent/tweet?text=' + encodeURIComponent('"' + arrQuotes[index] + '" ' + arrAuthor[index]));
});
@import url('https://fonts.googleapis.com/css?family=Playfair+Display');
*{
margin: 0;
box-sizing: border-box;
padding: 0;
}
body{
margin: 0;
background-color: #0EB022;
}
.container{
padding: 50px;
position: absolute;
height: auto;
width: 70%;
background-color: #FFF;
margin: auto;
left: 50%;
top: 50%;
transform:translate(-50%,-50%);
}
#default{
font-size: 30px;
font-weight: bold;
}
.content{
height: auto;
text-align: center;
font-size: 30px;
font-family: 'Playfair Display', serif;
}
.author{
height: auto;
text-align: right;
font-size: 20px;
color: #0EB022;
}
#b1{
width: 100px;
padding: 10px;
float: right;
}
.fa {
border: 0px;
padding: 3px;
margin: 3px;
font-size: 30px;
width: 40px;
text-align: center;
text-decoration: none;
border-radius: 3px;
}

.fa:hover {
opacity: 0.7;
}
.fa-twitter {
background: #0EB022;
color: white;
}
.fa-tumblr {
background: #0EB022;
color: white;
}
#quote{
float: right;
height: 36px;
width: auto;
margin: 3px;
padding: 10px;
color: #FFF;
border-radius: 2px;
border-color: transparent;
border: 0;
background-color: #0EB022;
}
#quote:hover {
opacity: 0.7;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="content">
<p id="default">Press 'New Quote'</p>
</div>
<br>
<div class="author">
</div>
<br><br>
<a class="button fa fa-twitter" id="tweet-quote" title="Tweet this quote on Twitter" target="_blank"></a>
<a class="button fa fa-tumblr" title="Post this quote on tumblr"></a>
<button class="button" id="quote">New Quote</button>
</div>

为什么 $("#tweet-quote").attr('href', 'https ://twitter.com/intent/tweet?text=' +encodeURIComponent('"' + arrQuotes[index] + '"' + arrAuthor[index])); 给我未定义的值?我如何访问它们?

最佳答案

您在 dom 加载之后赋值并在 dom 加载之前打印。请参阅下面的示例以了解。

var x=1;
$(document).ready(function(){
x=9;
console.log("After document Load :",x);
});
console.log("Before document Load :",x);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

已更新只需将 #tweet-quote 放入 #quote 点击事件

$(document).ready(function(){
var arrQuotes=[], arrAuthor = [], colors=['#132145','#800000','#990404','#008080','#16a085','#AEBF4E','#27ae60','#ffc0cb', '#2c3e50', '#f39c12', '#e74c3c', '#9b59b6', '#FB6964', '#342224', "#472E32", "#BDBB99", "#77B1A9", "#73A857"], index;
$.getJSON(
"https://codepen.io/coder_at_work/pen/dVPxBm.js",
function(data) {
data.forEach(function(object){
arrQuotes.push(object["quote"]);
arrAuthor.push(object["author"]);
});
}
);

$("#quote").on("click",function(){
index = Math.floor(Math.random()*arrQuotes.length);
currentQuote = arrQuotes[index];
currentAuthor = arrAuthor[index];
$(".content").html("<span class='fa fa-quote-left'></span>"+arrQuotes[index]);
$(".author").html("<span>- </span>"+arrAuthor[index]);
var c= Math.floor(Math.random()*colors.length);
$("body").animate({
backgroundColor: colors[c],
color: colors[c]
},1500);
$(".button").animate({
backgroundColor: colors[c]
},1500);
$(".author").animate({
color: colors[c]
},1500);
$("#tweet-quote").attr('href', 'https://twitter.com/intent/tweet?text=' + encodeURIComponent('"' + arrQuotes[index] + '" ' + arrAuthor[index]));
console.log('https://twitter.com/intent/tweet?text=' + encodeURIComponent('"' + arrQuotes[index] + '" ' + arrAuthor[index]));
});





});

@import url('https://fonts.googleapis.com/css?family=Playfair+Display');
*{
margin: 0;
box-sizing: border-box;
padding: 0;
}
body{
margin: 0;
background-color: #0EB022;
}
.container{
padding: 50px;
position: absolute;
height: auto;
width: 70%;
background-color: #FFF;
margin: auto;
left: 50%;
top: 50%;
transform:translate(-50%,-50%);
}
#default{
font-size: 30px;
font-weight: bold;
}
.content{
height: auto;
text-align: center;
font-size: 30px;
font-family: 'Playfair Display', serif;
}
.author{
height: auto;
text-align: right;
font-size: 20px;
color: #0EB022;
}
#b1{
width: 100px;
padding: 10px;
float: right;
}
.fa {
border: 0px;
padding: 3px;
margin: 3px;
font-size: 30px;
width: 40px;
text-align: center;
text-decoration: none;
border-radius: 3px;
}

.fa:hover {
opacity: 0.7;
}
.fa-twitter {
background: #0EB022;
color: white;
}
.fa-tumblr {
background: #0EB022;
color: white;
}
#quote{
float: right;
height: 36px;
width: auto;
margin: 3px;
padding: 10px;
color: #FFF;
border-radius: 2px;
border-color: transparent;
border: 0;
background-color: #0EB022;
}
#quote:hover {
opacity: 0.7;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="content">
<p id="default">Press 'New Quote'</p>
</div>
<br>
<div class="author">
</div>
<br><br>
<a class="button fa fa-twitter" id="tweet-quote" title="Tweet this quote on Twitter" target="_blank"></a>
<a class="button fa fa-tumblr" title="Post this quote on tumblr"></a>
<button class="button" id="quote">New Quote</button>
</div>

关于javascript - 在JS中访问ready函数之外的变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46252378/

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