报告 JavaScript 错误

huangapple go评论69阅读模式
英文:

Reporting JavaScript BUG

问题

我目前正在进行一个涉及多种编程语言的项目,包括HTML/JS/PHP。

在我的一个PHP函数中,我发现了一个意外的行为。

我将它分解成更简单的函数,以便更清晰:

<?php

function generateInput($inputID, $oninputFunction)
{
    echo '<input id="' . $inputID . '" oninput="' . $oninputFunction . '">';
}

function generateDIV(){
    $ID = "someId";
    echo '<div>';
    generateInput($ID, 'someFunction(' . $ID . ')');
    echo '</div>';
}
?>
<script>
  function someFunction(selectID) {
    console.log(selectID);
  }
</script>
<!DOCTYPE html>
<html>
<body>
    <?php generateDIV() ?>
</body>
</html>

正如您可以看到的,当在输入框中输入内容时,预期的控制台输出应为 "someId"。
然而,由于某种原因,输出是HTML元素,如下所示:

<input id="someId" oninput="someFunction(someId)">

如果有人能解释这种行为,请告诉我。

英文:

I am currently working on a project involving multiple languages including HTML/JS/PHP.

In one of my PHP functions, I discovered an unexpected behavior.

I broke it down to simpler functions so it would be clearer :

&lt;?php

function generateInput($inputID, $oninputFunction)
{
    echo &#39;&lt;input id=&quot;&#39; . $inputID . &#39;&quot; oninput=&quot;&#39; . $oninputFunction . &#39;&quot;&gt;&#39;;
}

function generateDIV(){
    $ID = &quot;someId&quot;;
    echo &#39;&lt;div&gt;&#39;;
    generateInput($ID, &#39;someFunction(&#39;. $ID .&#39;)&#39;);
    echo &#39;&lt;/div&gt;&#39;;
}
?&gt;
&lt;script&gt;
  function someFunction(selectID) {
    console.log(selectID);
  }
&lt;/script&gt;
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;body&gt;
    &lt;?php generateDIV() ?&gt;
&lt;/body&gt;
&lt;/html&gt;

As you can tell, the expected console output when something is typed in the input is "someId".
However, for some reason, the output is the HTML element which is:

&lt;input id=&quot;someId&quot; oninput=&quot;someFunction(someId)&quot;&gt;

If anyone has an explanation to this behavior, please let me know.

答案1

得分: 2

如您所见,当在输入框中键入内容时,预期的控制台输出为"someId"。

不,不是这样的。仔细查看由此生成的HTML:

<input id="someId" oninput="someFunction(someId)">

具体来说,JavaScript 函数调用如下:

someFunction(someId)

在这种情况下,someId 不是一个字符串,而是一个变量。巧合的是,这个变量名与元素的 id 匹配,因此它实际上是 window 对象上的一个属性,它引用了该元素。

(注:这种行为在这种情况下发生是巧合,但不应该依赖它作为一种标准行为。更多信息可以在这里找到。)

因此,代码输出的是该元素。

如果从元素中删除 id,则会出现错误:

Uncaught ReferenceError: someId is not defined

如果您希望它是一个字符串文字,那么它需要用引号括起来:

generateInput($ID, "'someFunction('" . $ID . "')'");

另外... 当尝试将多种语言混合在同一行代码中时,像这样的引号问题非常常见。PHP、HTML 和JS 混在一起就像这样容易导致引号问题。最好尽量将它们分开,以便更清晰地观察每种语言的语法。

英文:

> As you can tell, the expected console output when something is typed in the input is "someId".

No it isn't. Look closely at the HTML being generated by this:

&lt;input id=&quot;someId&quot; oninput=&quot;someFunction(someId)&quot;&gt;

Specifically the JavaScript function call:

someFunction(someId)

In this case someId is not a string. It's a variable. Coincidentally this variable name matches the id of the element, so it's actually a property on the window object which references that element.

(Note: This behavior is happening by coincidence in this case, but it shouldn't be relied upon as a standard. More information can be found starting here.)

So the code is outputting the element.

Remove the id from the element and observe an error:

> Uncaught ReferenceError: someId is not defined

If you want it to be a string literal then it needs quotes around it:

generateInput($ID, &quot;someFunction(&#39;&quot;. $ID .&quot;&#39;)&quot;);

As an aside... Quoting problems like this are very common when trying to mix multiple languages all on the same line of code. PHP, HTML, and JS all munged together like this is a recipe for quoting problems. It's best to keep them as separated as you can so you can more clearly observe the syntax of each language.

huangapple
  • 本文由 发表于 2023年6月16日 01:55:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76484329.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定