好的,Ajax。但是,除了ajax之外,还有其他解决方案吗?你不能把一个字体很棒的图标变成一个按钮,你能吗?如果你不知道我说的是什么当我说一个字体很棒的图标时这就是我的意思:
<i id="like1" onClick="like(this);" class="fa fa-thumbs-o-up">5</i>
言归正传。我想让它只要你点击这个图标,它运行一个php脚本,其中添加一个+1的文本文件,但你会如何运行,而不使用ajax和不刷新页面。我正在工作的网站是一个随机的youtube视频网站,人们将能够对视频进行评级。我想保存当有人点击大拇指按钮,但我不能刷新页面,否则它会去另一个随机的视频。现在你看到我的问题了。然而,如果没有ajax是不可能做到的,那么请解释ajax的解决方案,我没有足够的经验与ajax做我自己。这里有一些我已经写过的代码。
JS:
<script>
function like(obj) {
if (obj.style.color === "green") {
// set obj color to something other than green
obj.style.color = "white";
// get the innerText for the object that was clicked, parse as int, decrement by 1
obj.innerText = parseInt(obj.innerText) - 1;
}
else {
// we are incrementing, so check the dislike and decrement if necessary
var dislike = document.getElementById("dislike1");
if (dislike.style.color === 'red') {
dislike.style.color = 'white';
dislike.innerText = parseInt(dislike.innerText) - 1;
}
// set the colour of the object that was clicked
obj.style.color = "green";
// get the innerText for the object that was clicked, parse as int, and increment by 1
obj.innerText = parseInt(obj.innerText) + 1;
}
}
</script>
<script>
function dislike(obj) {
if (obj.style.color === "red") {
// set obj color to something other than green
obj.style.color = "white";
// get the innerText for the object that was clicked, parse as int, decrement by 1
obj.innerText = parseInt(obj.innerText) - 1;
}
else {
// we are incrementing, so check the dislike and decrement if necessary
var like = document.getElementById("like1");
if (like.style.color === 'green') {
like.style.color = 'white';
like.innerText = parseInt(like.innerText) - 1;
}
// set the colour of the object that was clicked
obj.style.color = "red";
// get the innerText for the object that was clicked, parse as int, and increment by 1
obj.innerText = parseInt(obj.innerText) + 1;
}
}
</script>
不喜欢,像按钮:
<strong><i id="like1" onClick="like(this);" class="fa fa-thumbs-o-up">5</i>
<i id="dislike1" onClick="dislike(this);" class="fa fa-thumbs-o-down">1</i> <br/></strong>
编辑:这是正确的吗?
<script>
function like(obj) {
var jqxhr = $.ajax(
type: "POST",
url: "liketest.php", //Name of your current file name
data: shareline //Here pass some parameters. Detect it in your php file before proceeding furthur. These parameters are available in post global variable when you send an ajax call.
)
.done(function(msg ) {
echo "Ajax seems fine";
})
.fail(function() {
echo "Failed";
})
});
if (obj.style.color === "green") {
// set obj color to something other than green
obj.style.color = "white";
// get the innerText for the object that was clicked, parse as int, decrement by 1
obj.innerText = parseInt(obj.innerText) - 1;
}
else {
// we are incrementing, so check the dislike and decrement if necessary
var dislike = document.getElementById("dislike1");
if (dislike.style.color === 'red') {
dislike.style.color = 'white';
dislike.innerText = parseInt(dislike.innerText) - 1;
}
// set the colour of the object that was clicked
obj.style.color = "green";
// get the innerText for the object that was clicked, parse as int, and increment by 1
obj.innerText = parseInt(obj.innerText) + 1;
}
}
</script>
不可以在客户端计算机上执行任何php脚本。PHP是一种服务器端脚本语言,它在服务器端是唯一可执行的。如果您不想刷新页面,则必须使用ajax函数。
使用jQuery ajax函数在文本文件中保存值
您可以在以下网站获得更多关于ajax函数的帮助。以下是jquery的语法:
var jqxhr = $.ajax(
type: "POST",
url: "pageFileName.php", //Name of your current file name
data: data //Here pass some parameters. Detect it in your php file before proceeding furthur. These parameters are available in post global variable when you send an ajax call.
)
.done(function(msg ) {
// Here you will put the code which will execute after successful ajax call. msg variable contain data which is received from your php script file.
})
.fail(function() {
// Code if ajax call fail
})
});
我没有尝试过它自己(除了在nodejs与Socket.io),但如果你不想使用ajax调用,出于某种原因,你可以尝试WebSockets。
这里的简短介绍:MDN WebSockets API