Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript onclick事件上未出现弹出窗口_Javascript_Php - Fatal编程技术网

Javascript onclick事件上未出现弹出窗口

Javascript onclick事件上未出现弹出窗口,javascript,php,Javascript,Php,为什么这段代码在PHP中不起作用?我想在用户单击id为“1”的我的图像时弹出一个对话框 回声“ $id1=document.getElementById('1'); $id1.onclick=function(){ 警惕(“你好”); }; "; 函数一(){ 警惕(“你好”); }; 点击我 与问题的第一条注释相反,您可以使用数字作为唯一标识符: id属性指定其元素的唯一标识符(id) 对于身份证的形式没有其他限制in 特别是,ID可以只包含数字,以数字开头, 以下划线开头,仅由标点符号等

为什么这段代码在PHP中不起作用?我想在用户单击id为“1”的我的图像时弹出一个对话框


回声“
$id1=document.getElementById('1');
$id1.onclick=function(){
警惕(“你好”);
};
";
函数一(){
警惕(“你好”);
};
点击我
  • 与问题的第一条注释相反,您可以使用数字作为唯一标识符:
  • id属性指定其元素的唯一标识符(id)

    对于身份证的形式没有其他限制in 特别是,ID可以只包含数字,以数字开头, 以下划线开头,仅由标点符号等组成

    元素的唯一标识符可用于多种用途, 最值得注意的是,使用 片段,作为脚本编写时针对元素的一种方式,以及 从CSS中设置特定元素的样式

    只需尝试放置一个属性为
    id=“1”
    input
    元素,然后使用
    document.getElementById
    函数访问它。它起作用了

    但我同意的是,这些标识很难阅读、理解甚至记住。。。只有“1”不能告诉你关于元素的任何信息。我不建议使用这种方法。是的,最好使用更多的“告诉”名称,如“测试按钮”或类似的smth

  • 代码不起作用,因为在PHP标记的上下文中,您使用了
    $
    符号,该符号引用了一个变量。。。PHP变量。因此,行
    $id1=document.getElementById('1')
    被解释为
    =document.getElementById('1')并导致JS的语法错误。它首先作为一个php变量进行计算,因为它没有定义,所以没有什么可响应的,所以您要添加到页面的js脚本没有完成
  • 页面该部分的源代码如下所示:

    <script type="text/javascript">
     = document.getElementById('1');
    .onclick = function() {
                        alert('hello');
                    };
    </script>
    
    
    =document.getElementById('1');
    .onclick=函数(){
    警惕(“你好”);
    };
    
    你看,没有变量本身

    请尝试以下组合代码,而不是此代码:

    <?php
    /*
        If you declare a php variable, the value you assign to it becomes a JS 
        variable name and is echoed then in the script
    */
    $id1 = "test";
    echo "<script type='text/javascript'>
    $id1 = document.getElementById('1');
    $id1.onclick = function() {
                    alert('hello');
                };
    </script>"
    
    ?>
    

    不能在元素上使用数字作为ID。使用elem1之类的工具,甚至更好的工具descriptive@PatrickHund你说得不对好吧,我很惊讶你居然可以用数字作为ID。我认为这是一个很好的做法,这是另一个重要的问题。为这个愚蠢的错误感到抱歉。我是新来的。我知道把id作为数值不是一个好主意,但它背后有一个目的。无论如何,再次谢谢你^。^希望你不受欢迎。如果你愿意,你可以投票赞成我的答复。我想强调的是,使用“1”作为id并不是代码无法运行的根本原因。你说得对!我试着投你的票,但我没有至少15点的声望。2天前刚注册LOL:)
    
    <?php
    /*
        If you declare a php variable, the value you assign to it becomes a JS 
        variable name and is echoed then in the script
    */
    $id1 = "test";
    echo "<script type='text/javascript'>
    $id1 = document.getElementById('1');
    $id1.onclick = function() {
                    alert('hello');
                };
    </script>"
    
    ?>
    
    <script type="text/javascript">
    // Var name has appeared!
    test = document.getElementById('1');
    test.onclick = function() {
                        alert('hello');
                    };
    </script>