如何在通过函数显示的数组中放置链接

时间:2022-11-23 11:36:52

Ok so I have a textbox where, depending on which button you click, text from an array is displayed

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

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>

So far so good, everything works as planned, but now I want to use a link like "Text[1]="for more informations look <a style="color:blue;" href="insertlinkhere">here</a>” and it just stops working (aka nothings happening when I click a button.

到目前为止一切都那么好,一切都按计划进行,但现在我想使用像“Text [1] =”这样的链接来获取更多信息,看看这里“它只是停止工作(当我点击一个按钮时,就会发生这种情况。

Help pls?

4 个解决方案

#1


0  

You need to escape occurance of " as \"

你需要摆脱“as”的出现

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

#2


0  

You need to escape the quotes, like \" or use single quotation marks ' outside.

您需要转义引号,例如\“或在外部使用单引号。

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>

#3


0  

you have problem with the quote, you can fix it by changing the quote for the attributes. JS can differentiate the quotes and your link will start working. so it will be "Text[1]=for more informations look here”

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

#4


0  

As the attributes of HTML elements are in double quotes, putting the entire string in single quotes can solve the problem.

由于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>

#1


0  

You need to escape occurance of " as \"

你需要摆脱“as”的出现

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

#2


0  

You need to escape the quotes, like \" or use single quotation marks ' outside.

您需要转义引号,例如\“或在外部使用单引号。

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>

#3


0  

you have problem with the quote, you can fix it by changing the quote for the attributes. JS can differentiate the quotes and your link will start working. so it will be "Text[1]=for more informations look here”

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

#4


0  

As the attributes of HTML elements are in double quotes, putting the entire string in single quotes can solve the problem.

由于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>