简体   繁体   English

如何将javascript代码包含到php视图中?

[英]How to include javascript code into php view?

I have to print news.php code out registering it into a variabile to put inside a grid in another view. 我必须打印news.php代码,将其注册为可变变量,以放入另一个视图的网格中。 My main problem is that the code in news.php doesn't show anything, after compilation it is just blank though I'm quite sure the script is working. 我的主要问题是news.php中的代码未显示任何内容,尽管我确定脚本可以正常工作,但编译后只是空白。

I've already tried to convert news.php into news.js but nothing changed, I guess there are problems in the registerJs(). 我已经尝试将news.php转换为news.js,但是没有任何变化,我想registerJs()中存在问题。

news.php news.php

<head>
<script type="text/javascript" 

src="http://islab.di.unimi.it/content/islab_site/media/com_islabteachpages/js/jquerynews.js"></script>
<script type="text/javascript" src="http://islab.di.unimi.it/content/islab_site/media/com_islabteachpages/js/jquery.jfeed.pack.js"></script>
<script type="text/javascript">
    jQuery(function() {
        jQuery.getFeed({
            url: 'http://islab.di.unimi.it/iNewsMail/feed.php?channel=bdlab1',
            success: function(feed) {
                var html = '';
                for(var i = 0; i < feed.items.length && i < 10; i++) {

                    var item = feed.items[i];
                    html += '<div class="body-news">'
                        + '<b>'
                        + item.title
                        + '</b><br />'
                        + '<div>'
                        + item.description
                        + '</div>'
                        + ' </div>';
                }
                jQuery('#newsresult').append(html);
            }
        });
    });
</script>
</head>
<body>

<div id="newsresult">

</div>
</body>

view.php view.php

     # put the js script inside my $news variable
     $this->registerJsFile(
         '@web/views/news.php',
         View::POS_HEAD);
     );
     echo '<div class="uk-grid">
            <div class="uk-width-2-3">
                <div class="itp_middle uk-width-1-1 uk-width-medium-3-5">
                    <div class="itp_section-1 ">
                        <div class="text">'
                            . $rows['content'] . '
                        </div>
                    </div>
                </div>
            </div>
            <div class="uk-width-1-3">
                <div class="text">'
                    . $news . '
                </div>
            </div>
        </div>';

I expect the js code to be displayed inside the grid. 我希望js代码显示在网格内。 Every help would be much appreciated. 每一个帮助将不胜感激。

If you don't load jQuery in your page how can you execute the javascript code related to jQuery. 如果您没有在页面中加载jQuery,如何执行与jQuery相关的javascript代码。 It will directly throw error. 它将直接引发错误。 This kind of error you can easily solve just by opening the browser's console. 您只需打开浏览器的控制台即可轻松解决此类错误。

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

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