编写脚本时发生奇怪的事情

发布于 2024-11-27 14:46:50 字数 908 浏览 0 评论 0原文

我单击页面中的链接,然后 javascrpt 中的功能将完成 我有这个脚本:

<script>
  function useraccess()
{
var inputs = document.getElementsByTagName("input");
for (var i = 0; i < inputs.length; i++){
    if (inputs[i].type == "button"){
    inputs[i].style.visibility = "hidden";
                                   }
                                       }
}
</script>

并且形式为:

<?php
if(!$_SESSION['isadmin'])
{
 echo '<script>useraccess();</script>';
}
?>

如果我通过单击我编写的链接运行很短的时间,它将向我显示按钮,然后它们将被隐藏,但是!如果我在代码中包含我用此行编写的

<script type="text/javascript" src="file.js"></script>

: 我的代码没有问题,当我单击链接时,它会隐藏我的按钮,

我看不到“短时间后”它们会隐藏的按钮,

也看不到它们

我的意思是它们“正在”隐藏,即使很短的时间我 非常意外地解决了我的问题! 因为我没有 file.js!我只是尝试一些东西。

这对我来说很有趣,我很想知道原因是什么?

I click links in my page then my function in javascrpt will be done
I have this script:

<script>
  function useraccess()
{
var inputs = document.getElementsByTagName("input");
for (var i = 0; i < inputs.length; i++){
    if (inputs[i].type == "button"){
    inputs[i].style.visibility = "hidden";
                                   }
                                       }
}
</script>

and in the form:

<?php
if(!$_SESSION['isadmin'])
{
 echo '<script>useraccess();</script>';
}
?>

If I run it by clicking links that I wrote for a very short time it will show me the buttons and then they will be hide but! if I Include this line previous of <script> that I wrote

<script type="text/javascript" src="file.js"></script>

with this line in codes:
No problem with my code and when I click links it will hide my buttons

I can't see the buttons that "after short time" they will hide

I mean they "are" hide I can't see them even for a short time

I solved my problem very accidentally!
cause I don't have file.js! I was just try something.

It is so interesting for me I have eager to know what is the reason?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心头的小情儿 2024-12-04 14:46:50

您的 PHP 代码将在 HTML 页面中生成以下代码:

<脚本>useraccess();

这将在页面加载时运行,并将隐藏所有按钮。它发生的确切时间取决于您将该行放置在页面上的位置。但运行它与任何按钮没有连接。

既然您谈到放入表单,我猜测您认为以某种方式使 PHP 代码生成 Javascript 会影响您的页面。不会的。当您的浏览器显示您的页面时,PHP 已完成。您需要使用一些 JavaScript 来观察按钮的点击情况。

(如果我误解了你的意图,我很抱歉)。

OP回复后编辑

哦,我明白了。所以您确实希望按钮从一开始就隐藏,但它们首先出现然后被隐藏?

我认为问题是“useraccess”函数直到页面加载带有按钮的表单后才运行。

更好的方法是更改​​ PHP,以便根本不输出按钮,或者禁用输出按钮。如果用户没有特权,那么它们根本不会出现在页面上。

Your PHP code will generate the following code in your HTML page:

<script>useraccess();</script>

This will be run when the page loads, and will hide all your buttons. Exactly when it happens will depend where on the page you put that line. But running it has no connection with any buttons.

Since you talk about putting in the form, I'm guessing that you think that somehow making your PHP code generate that Javascript will affect your page. It won't. When your browser is displaying your page, the PHP has finished. You need to use some Javascript to observe the button clicks.

(If I've misunderstood your intention, I'm sorry).

Edit after reply from the OP:

Oh, I see. so you do want the buttons hidden right from the start, but they were appearing at first and then being hidden?

I think the problem then is that the 'useraccess' function is not run until after the page has loaded the form with its buttons.

A much better approach would be to change your PHP so as not to output the buttons at all, or output them disabled. Then they would not appear on the page at all, if the user was not privileged.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文