JavaScript进入Drupal模块

I'm trying to implement a JavaScript script into a Drupal 7 module and for some reason I keep getting this error here: "Parse error: syntax error, unexpected T_ECHO, expecting ')'"

I've been attempting this for a few hours now, but I'm just at a loss. Any help would be so greatly appreciated.

My code block indented to the far right. The other code is part of a module in Drupal 7 for your reference.

$node->content['actions'] = array(
      '#theme' => 'links',
      '#prefix' => '<div id="match-actions">',
      '#suffix' => '</div>',
      '#links' => _match_actions($node),
                    echo '<script type="text/javascript">'
                        , 'croll();'
                        , 'troll();'
                        , '</script>';
      '#attributes' => array(
        'class' => array('links', 'match-actions'),
      ),
      '#heading' => t('Match actions'),
      '#weight' => -10,
    );

The JavaScript I'm trying to insert (as you can see my attempt in the echo above) is

function class_roll() {
    // Car Classes
    var classes = ["B", "A", "S", "R3", "R2", "R1"],
        classToUse = classes[Math.floor(Math.random() * classes.length)];
    document.getElementById("croll").innerHTML = classToUse ;
}

function track_roll() {

    var tracks = ["Clear Springs Circuit", "Festival North Circuit", "Beaumont Circuit", "Finley Dam Circuit", "Gladstone Circuit", "Clifton Valley Trail", "Gladstone Trail", "Red Rock Descent", "Red Rock Hill Climb"],
        trackToUse = tracks[Math.floor(Math.random() * tracks.length)];
    document.getElementById("troll").innerHTML = trackToUse ;
}

What exactly am I doing wrong? I've been searching around Stack and all over the net which has enabled me to try different syntaxes, but I just can't get it to work. I'm not an expert in JS and PHP, but I'm trying to learn :). Again, any help is REALLY appreciated.

P.S. - In HTML terms, what I'm trying to do is this:

<p id="croll">Some text here</p>
<p id="troll">Some text here</p>
    <button onclick="class_roll(); track_roll();">Class Roll</button>

but it would be great if instead of doing an onclick type of PHP action, it would be an onload type of event, but it would only onload for the first time and stick there and remain static.

3个回答



你不能在数组中放入回声。</ p>

你应该能够 要做:</ p>

  $ links = _match_actions($ node); 
$ links [] ='&lt; script type =“text / javascript”&gt; 克罗尔(); 巨魔(); &lt; / script&gt;'

$ node-&gt; content ['actions'] = array(
'#theme'=&gt;'links',
'#prefix'=&gt;'&lt; div id =“match-actions”&gt;',
'#suffix'=&gt;'&lt; / div&gt;',
'#links'=&gt; $ links,
'#attributes'=&gt;数组 (
'class'=&gt; array('links','match-actions'),
),
'#heading'=&gt; t('匹配动作'),
'#weight'= &gt; -10,
);
</ code> </ pre>
</ div>

展开原文

原文

You can't put an echo inside of an array.

You should just be able to do:

$links = _match_actions($node);
$links[] = '<script type="text/javascript"> croll(); troll(); </script>'

$node->content['actions'] = array(
      '#theme' => 'links',
      '#prefix' => '<div id="match-actions">',
      '#suffix' => '</div>',
      '#links' => $links,
      '#attributes' => array(
        'class' => array('links', 'match-actions'),
      ),
      '#heading' => t('Match actions'),
      '#weight' => -10,
    );

doufei8691
doufei8691 新的$ links语法允许我的网站再次使用我的OP中提到的解析错误。 非常感谢! 还有办法<p id =“croll”>这里的一些文字</ p> <p id =“troll”>这里的一些文字</ p> <button onclick =“class_roll(); track_roll();” > Class Roll </ button>但在我列出的PHP中? 再次感谢你的帮助!
接近 8 年之前 回复



正如HorusKol所说,你无法直接调用模块中的JavaScript函数。 模块的原因是用PHP编写的,并且不能将其他编程语言的函数调用为一个。 </ p>

如果您想插入JavaScript代码,您应该使用函数 drupal_add_js ()这样做。</ p>

因此,您可以用以下内容替换 echo </ code> :</ p>

  echo drupal_add_js('croll(); troll();','inline'); 
</ code> </ pre>
</ div>

展开原文

原文

As HorusKol said you cannot directly call a JavaScript function inside your module. The reason being modules are written in PHP, and one cannot call functions of other programming language into one.

If you wish to insert JavaScript code, you should use the function drupal_add_js() to do so.

So, you could replace your echo with the following:

echo drupal_add_js('croll();troll();','inline');

dpikoto468637
dpikoto468637 由于某种原因,这是打破网站,并给我在我的OP中提到的相同的解析错误:解析错误:语法错误,意外的T_ECHO,期待')'我应该将它添加为#body(如果有这样的东西) )或#heading是否更容易? drupal_add_js有可能吗? 到目前为止,非常感谢您的帮助!
接近 8 年之前 回复



更好的方法,不会破坏Drupal AJAX之类的东西,就是使用Drupal行为。 </ p>

 (function($){
Drupal.behaviors.myModuleName = {
attach:function(context,settings){
$(“。match-actions” ,context).once('match-actions',function(){
croll();
troll();
})
}

}
})(jQuery);
</ code> </ pre>

将其放在js文件中并使用drupal_add_js加载(drupal_get_path('module','{my module name}')。'{js file name}') ; </ p>
</ div>

展开原文

原文

The better method, that won't break with things like Drupal AJAX, is to use Drupal behaviors.

(function ($) {
  Drupal.behaviors.myModuleName = {
    attach : function (context, settings) {
       $(".match-actions", context).once('match-actions', function(){
         croll();
         troll();
       })
    }

  }
})(jQuery);

Place that in a js file and load it with drupal_add_js(drupal_get_path('module', '{my module name}') . '{js file name}');

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐