Offset()无法使用Yii2处理我的应用程序

时间:2016-01-04 01:52:56

标签: jquery html yii2

Jquery的

$(document).ready(function(){
  $("button").click(function(){
    var x = $("#pp").offset();
    alert("Top: " + x.top + " Left: " + x.left);
  });
});

HTML

$this->registerJsFile('time/web/js/jquery.min.js', ['depends' => [\yii\web\JqueryAsset::className()]]);

<p id="pp">This is a paragraph.</p>

<button>Return the offset coordinates of the p element</button>

我从其他网站复制这些代码。我把它复制到另一个新的空文件,它在那里工作。我能知道是什么原因以及如何使其发挥作用?

1 个答案:

答案 0 :(得分:0)

将注册脚本文件路径更改为这样。

<?php

$this->registerJsFile(Yii::$app->request->baseUrl.'/js/jquery.min.js', ['depends' => [\yii\web\JqueryAsset::className()],'position'=>\yii\web\View::POS_HEAD]);

?>

<p id="pp">This is a paragraph.</p>

<button>Return the offset coordinates of the p element</button>