简体   繁体   English

如何在不使用zendx的情况下将zquery与zend框架一起使用?

[英]How do I use jquery with zend framework, without using zendx?

I tried this in the layout file 我在布局文件中尝试过

$(document).ready(function(){ alert("hello"); } ); $(document).ready(function(){alert(“ hello”);});

How do I get this simple thing working? 如何使这个简单的事情起作用? I included jquery in the layout file, using echo $this->headScript()->appendFile(....) 我使用echo $ this-> headScript()-> appendFile(....)将jquery包含在布局文件中。

Is there a way to use jquery as is? 有没有办法按原样使用jquery? Without using zendx? 不使用zendx?

What about the following in your layout file: 布局文件中的以下内容如何:

$this->headScript()->appendFile('/js/jquery.js'); 
$this->headScript()->appendScript('$(document).ready(function() { alert("hello"); });','text/javascript');

What I mostly do is: 我主要要做的是:

$this->headScript()->appendFile('/js/jquery.js');
$this->headScript()->appendFile('/js/application.js');
$this->headScript()->appendScript('application.init();', 'text/javascript');

echo $this->headScript();

Then the application.js: 然后是application.js:

var application = {
  init : function() {
    //do here anything you like
  }
}

If you want to call a specific function from let's say your controller you add this function to your application JS first: 如果要从控制器中调用特定函数,则应首先将此函数添加到应用程序JS中:

var application = {
  init : function() {
    //do here anything you like
  },
  bindChangeEventToSomething : function() {
    $('a').click(function() {});
  }
}

Then in your controller you could add in your action: 然后可以在控制器中添加操作:

$this->view->headScript->appendScript(
   'application.bindChangeEventToSomething()',
   'text/javascript'
);

You should be able to include jQuery without using ZendX. 您应该能够在不使用ZendX的情况下包含jQuery。 If you would like it to be included in every page add it in the bootstrap or in a controller if it's specific. 如果您希望将其包含在每个页面中,请将其添加到引导程序或特定于控制器的控制器中。 Here's an example of what putting it in the bootstrap would look like : 以下是将其放入引导程序的示例:

protected function _initView(){
    $this->bootstrap('layout');
    $layout = $this->getResource('layout');
    $view = $layout->getView();
    $view->headScript()->appendFile('/js/jquery.min.js');
}

If you wish to include a js file in just a single controller/action you can include it the init/action of the controller. 如果您只想在单个控制器/动作中包含一个js文件,则可以在控制器的init /动作中包含它。

$this->view->headScript()->appendFile('/js/index.js');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM