【问题标题】:How to run javascript code in any part of the html file?如何在 html 文件的任何部分运行 javascript 代码?
【发布时间】:2010-05-29 11:07:19
【问题描述】:

我正在使用一个 jQuery 通知系统,当用户点击一个链接时它会运行:

<a href="javascript:void(0);" onclick="$.n('This is a sample notification message, there are 3 more notification types which is all customizable through CSS.');">here</a>

但问题是我想运行 $.n('This is a sample notification') 而不是按链接。我想在 php 上的 If 语句发生某些事情时调用该事件。

例如

<?php if (condition) { DO THE NOTIFICATION; } ?>

【问题讨论】:

  • 那么,你想要一个页面加载事件?

标签: php javascript jquery html


【解决方案1】:

首先请记住,您的 php 代码是在服务器上评估的,而 JavaScript 和 jQuery 在浏览器中运行。评估发生在不同的时间、不同的地方。因此,您不能从 php 调用 JavaScript 函数。

但是,使用 php,您可以渲染 HTML 和 JavaScript 代码,以便仅在您的 php 条件为真时才渲染它。也许您可能想尝试这样的事情:

<?php 
if (condition) { 
   echo "<script>";
   echo "$.n('This is a sample notification message');";
   echo "</script>";
} 
?>

【讨论】:

    【解决方案2】:
    <?php if (condition){?>
    <script>
    $.n('This is a sample notification message, there are 3 more notification types which is all customizable through CSS.');
    </script>
    <?php } ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-03
      • 1970-01-01
      • 1970-01-01
      • 2016-03-24
      • 2015-07-13
      • 2012-05-12
      • 2020-07-02
      • 2010-11-13
      相关资源
      最近更新 更多