javascript Php里面的javascript函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11079735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 12:02:43  来源:igfitidea点击:

Php inside javascript function

phpjavascriptjquery

提问by Jasminder Pal Singh

What i am trying to do is inside script tags run javascript inside the php loops. For example:

我想要做的是在脚本标签内在 php 循环中运行 javascript。例如:

<script>
$('#mydiv').mouseover(function(){
time();
function time(){
<?php  
    $secs = 45; 
    $secs--;
if($secs <= 25){
?>//javascript code here 
}
});
</script>

My main purpose is when the user mouseover a div, a javascript function runs and inside that javascript function there is php if conitions. If the time is less then 25 then do a specified javascript code. then another condition if the time is less that 10 then do another javascript function.Any type of help will be appreciated. THanks

我的主要目的是当用户将鼠标悬停在一个 div 上时,一个 javascript 函数会运行,并且在该 javascript 函数内部有 php if conitions。如果时间小于 25,则执行指定的 javascript 代码。然后是另一个条件,如果时间少于 10,则执行另一个 javascript 函数。任何类型的帮助将不胜感激。谢谢

回答by Jānis

JavaScript is client-side script, PHP is server-side script. So, either you do AJAXcalls to server or just pure JS.

JavaScript 是客户端脚本,PHP 是服务器端脚本。因此,您要么对服务器进行AJAX调用,要么仅对纯 JS 进行调用。

AJAX+PHP:

AJAX+PHP:

<script>
    $('#mydiv').mouseover(function(){
        $.ajax({
            url: 'secs.php',
            success: function(data) {
                var secs = parseInt(data);

                if(secs <= 10) {
                    // do stuff
                }
                else if(secs <= 25) {
                    // do stuff
                }
            },
        });
    });
</script>

secs.php:

秒.php:

<?php
    session_start();

    if(!isset($_SESSION['secs'])) $_SESSION['secs'] = 45;
    else echo $_SESSION['secs'] --;
?>

Pure JS:

纯JS:

<script>
    var secs = 45;

    $('#mydiv').mouseover(function(){
        secs --;

        if(secs <= 10) {
            // do stuff
        }
        else if (secs <= 25) {
            // do stuff
        }
    });
</script>

回答by Quentin

PHP runs on the server and outputs some text.

PHP 在服务器上运行并输出一些文本。

The browser then interprets that text as HTML/JS/etc.

然后浏览器将该文本解释为 HTML/JS/等。

You can't run PHP in response to a client side event unless you issue a new HTTP request.

除非您发出新的 HTTP 请求,否则您无法运行 PHP 来响应客户端事件。

If you know the values that your PHP cares about at the time of page build, then you can generate JavaScript with that data stored in a variable.

如果您知道 PHP 在页面构建时关心的值,那么您可以使用存储在变量中的数据生成 JavaScript。

回答by nyson

You can't use PHP within Javascript, if you need server side operations you should use AJAX. jQuery has a neat implementation of it.

你不能在 Javascript 中使用 PHP,如果你需要服务器端操作,你应该使用 AJAX。jQuery 有一个简洁的实现。

回答by emte

you cannot do this because php run on server and javascript on client. It means that your php code is transformed into DHTML on server and send to client. But javascript runs on client... So if you want to combine both of them. you have to request server from client again (by ajax for example).

你不能这样做,因为 php 在服务器上运行,javascript 在客户端运行。这意味着您的 php 代码在服务器上转换为 DHTML 并发送到客户端。但是 javascript 在客户端上运行......所以如果你想将它们结合起来。您必须再次从客户端请求服务器(例如通过 ajax)。

But back to your code... why do you don't count time in javascript? you can get what you want by var date = new Date(); time = date.getCurrentTime()

但是回到你的代码......为什么你不在javascript中计算时间?你可以得到你想要的var date = new Date(); time = date.getCurrentTime()