【问题标题】:How do I put a link inside an array that is displayed through a function如何将链接放在通过函数显示的数组中
【发布时间】:2017-01-11 07:32:59
【问题描述】:

好的,我有一个文本框,根据您单击的按钮,将显示数组中的文本

var Text= new Array();

Text[1]="a"
Text[2]="b"
Text[3]="c"
function Anzeige(x) {
document.getElementById("Text").innerHTML = Text[x];
}
.......
<input type="button" value="x" onclick="Anzeige(1)">
<input type="button" value="y" onclick="Anzeige(2)">
<input type="button" value="z" onclick="Anzeige(3)">
............
<div id="Anzeigefeld">
<div id="Text"></div>
</div>

到目前为止一切顺利,一切都按计划进行,但现在我想使用“Text[1]="for more informations look &lt;a style="color:blue;" href="insertlinkhere"&gt;here&lt;/a&gt;”之类的链接,但它只是停止工作(当我单击按钮时什么也没有发生。

请帮忙?

【问题讨论】:

  • onclick 属性始终为 javascript。
  • 可能是关于引号的。试试'for more informations look &lt;a style="color:blue;" href="insertlinkhere"&gt;here&lt;/a&gt;'

标签: javascript html arrays hyperlink


【解决方案1】:

您需要避免出现“ as \”

Text[1]="for more informations look <a style=\"color:blue;\" href=\"insertlinkhere\">here</a>";

【讨论】:

    【解决方案2】:

    您需要对引号进行转义,例如\" 或在外面使用单引号'

    var text = [
            '<a style="color:blue;" href="http://example.com/">here</a>',
            "<a style=\"color:red;\" href=\"http://example.com/\">here</a>",
            "c"
        ];
    
    function anzeige(x) {
        document.getElementById("text").innerHTML = text[x];
    }
    <input type="button" value="x" onclick="anzeige(0)">
    <input type="button" value="y" onclick="anzeige(1)">
    <input type="button" value="z" onclick="anzeige(2)">
    <div id="anzeigefeld">
        <div id="text"></div>
    </div>

    【讨论】:

    • 我的错,没有发布我的原始解决方案,我知道并尝试了'',但由于某种原因它没有工作。但是 \" 为我做到了,所以感谢你和 Angel Maharjan
    【解决方案3】:

    您的报价有问题,您可以通过更改属性的报价来解决它。 JS 可以区分引号,您的链接将开始工作。 所以它会是 “Text[1]=更多信息看这里”

    【讨论】:

      【解决方案4】:

      由于 HTML 元素的属性是用双引号引起来的,所以将整个字符串放在单引号中可以解决问题。

      var Text = new Array();
      
      Text[1] = 'for more informations look <a style="color:blue;" href="insertlinkhere">here</a>'
      Text[2] = "b"
      Text[3] = "c"
      
      function Anzeige(x) {
      
        document.getElementById("Text").innerHTML = Text[x];
      }
      <input type="button" value="x" onclick="javascript:Anzeige(1)">
      <input type="button" value="y" onclick="Anzeige(2)">
      <input type="button" value="z" onclick="Anzeige(3)">
      <div id="Anzeigefeld">
        <div id="Text"></div>
      </div>

      【讨论】:

        猜你喜欢
        • 2019-11-21
        • 2021-07-26
        • 2011-02-02
        • 1970-01-01
        • 2020-06-29
        • 2022-08-07
        • 1970-01-01
        • 2020-07-12
        • 1970-01-01
        相关资源
        最近更新 更多