让PHP和JAVASCRIPT一起工作


Make PHP and JAVASCRIPT work together.

我有一个简单的php if语句,用于检查表单提交上的错误返回。

<?php if ($this->session->flashdata('error')): ?>
    <a href="#" class="close" data-dismiss="alert">&times;</a>
    <div class="alert alert-error" id="error"><?php echo $this->session->flashdata('error'); ?></div>
<?php endif ?>

我有这个脚本,我想把这个php当php为真时登录框或account-container将会震动。

$("#account-container").addClass("animated shake");

我试着在php内回显脚本,但当if为真时,所有这些都给了我echo ;显示在页面上。

也许你可以使用这个脚本,我不熟悉jquery,你必须适应代码:

$(function(){
    if ($("#error").length !== 0) {
         $("#account-container").addClass("animated shake");
    }
});

将其作为javascript源文件或嵌入到脚本标签

$().load将包含处理程序挂钩到页面*中的onload事件该函数只是检查页面中是否存在id为error和的元素如果可以的话,将类添加到#account-container元素中。

  • 我不会使用$().ready(),因为dom仍然可以部分渲染

引用:

http://api.jquery.com/ready/表示使用。load()函数进行onload事件和警告<body onload="something">标签与jquery事件管理不兼容

http://api.jquery.com/load/可以用

我认为你正在使用Jquery javascript震动,你应该调用javascript例程时,页面准备加载

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>shake demo</title>
  <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
  <style>
  #toggle {
    width: 100px;
    height: 100px;
    background: #ccc;
  }
  </style>
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
</head>
<body>
<p>Click anywhere to shake the box.</p>
<div id="toggle">This is rendered by PHP</div>
<script>
$( document ).ready(function() {
  $( "#toggle" ).effect( "shake" );
});
</script>
</body>
</html>

请注意,整个页面是由PHP呈现的,当文档呈现准备好时,脚本将工作。