如何在Framework7中使用JS

How to use JS in Framework7?

本文关键字:JS Framework7      更新时间:2023-09-26

我用framework7创建了一个应用程序。现在我尝试在page-content中执行一个javascript,但它没有执行。

<div class="pages">
<div class="page  close-panel" data-page="item">
    <div class="page-content">
        <div class="content-block-title">Title</div>
        <script type="text/javascript">
          alert("testoutput"); // no alert
          console.log("TEST"); // no log
        </script>
    </div>
</div>

如何运行此javascript代码?

更新

该页面是从其他HTML页面加载的。

使用回调(onPageInit)执行代码

在此之前,我从未听说过Framework7,但在查看了文档后,我不相信您能够以这种方式使用Javascript。

看起来,对于JS事件,您必须在Framework7构造函数中确定事件的范围:

var myApp = new Framework7();
var $$ = Dom7;
$$('.alert-text').on('click', function () {
    myApp.alert('Here goes alert text');
});

当然,上面的例子直接取自F7文档,并且依赖于单击事件,但您可以尝试将警报事件作为myApp的一种方法,看看它是否适用于您。

var myApp = new Framework7();
//Add callback function that will be executed when Page with data-page="about" attribute will be initialized
myApp.onPageInit('dashboard', function (page) {
    console.log('dashboard page initialized');
    console.log(page);
});
// Option 2. Using live 'page:init' event handlers for each page (not recommended)
$$(document).on('page:init', '.page[data-page="dashboard"]', function (e) {
    console.log('dashboard loaded with page:init');
    createGraph();
});

以上内容对我很有帮助。。尽管跟随不起作用。

myApp.onPageInit('dashboard', function (page) {
    console.log('dashboard page initialized');
    console.log(page);
});

如果您在index.html文件中编写了任何javascript代码,请将该代码放入

    <head> 
      <script>
         alert("testoutput"); // no alert
         console.log("TEST"); // no log
      </script>
    </head> like this, which is defined in index.html file 
Or ,if you want write JS code for some particular html file ,
Do like this
 <div class="page  close-panel" data-page="item">
 <div class="page-content">
    <div class="content-block-title">Title</div>
</div>
    <script>
      alert("testoutput"); // no alert
      console.log("TEST"); // no log
    </script>
 </div>