简体   繁体   English

如何将html链接插入数组字符串?

[英]how to insert an html link into an array string?

a bit confused here. 这里有点困惑。 I have an array that's full of answers to questions and am having trouble getting the link to appear as.. an actual link. 我有一个数组,里面有很多问题的答案,并且很难使链接显示为实际链接。 I've tried document.write but it's just breaking my page. 我已经尝试了document.write,但它只是破坏了我的页面。 Any ideas? 有任何想法吗?

this.answer = [
    'answer1' + '<a href="URL">click here</a>',
    'answer 2', 
    'answer 3'
]

I have no idea how to write the <a href="URL">click here</a> bit to get it to not appear as just a string of text instead of HTML. 我不知道如何编写<a href="URL">click here</a>位,以使其不会仅显示为文本字符串而不是HTML。 Any ideas? 有任何想法吗? My array is in a Shadow DOM custom HTML5 element, also. 我的数组也在Shadow DOM自定义HTML5元素中。 I know how to target the shadow DOM, just not sure of the basic format.. 我知道如何定位影子DOM,只是不确定基本格式。

Thanks! 谢谢! - Shan -山

edit: Thanks for the response, mplungjan. 编辑:感谢您的答复,mplungjan。 Like this? 像这样?

this.answer = [
    'answer1' + innerHTML('<a href="URL">click here</a>'),
    'answer 2', 
    'answer 3'
]

this makes the entire thing appear blank on my page, however.. Think I'm formatting it wrong 这会使整个页面在我的页面上显示为空白。。以为我的格式错误

edit 2 okay trying to fix it based on the answers I got.. I tried this: 编辑2好的,尝试根据我得到的答案修复它。

 var insertThis = document.getElementById('myElement').innerHTML = '<a href="#">Click here</a>';

 this.answer = [
    'answer1' + insertThis,
    'answer 2', 
    'answer 3'
]

but it's still making my array totally blank :( sorry I am pretty new to arrays it is very confusing to me.. Do i need to put the entirety of my first array item in the innerHTML including the 'answer1' bit? 但是它仍然使我的数组完全空白:(抱歉,我对数组很陌生,这让我很困惑。我是否需要将我的第一个数组项的全部内容都放入innerHTML中,包括“ answer1”位?

edit it says that I cannot set innerHTML of null.. but when I console log my element right before I get the element by id it logs it fine.. hmm 编辑它说我不能将innerHTML设置为null ..但是当我在通过id获取元素之前立即控制台记录我的元素时,它会很好地记录它。

Like the comments to your post, the text isn't being processed as HTML when you're putting it in the DOM of your page. 就像帖子中的评论一样,当您将文本放在页面的DOM中时,文本也不会被处理为HTML。

You need to set the innerHTML of your element to be a value of the array, like this: 您需要将元素的innerHTML设置为数组的值,如下所示:

    var answer = [
        'answer1' + '<a href="URL">click here</a>',
        'answer 2', 
        'answer 3'
    ];

    document.getElementById("yourelement").innerHTML = answer[0];

Try this, 尝试这个,

var aColor = [
    '<a href="#red">red</a>',
    '<a href="#green">green</a>',
    '<a href="#blue">blue</a>'
];

var x = Math.floor(Math.random() % 3);

document.getElementById('elementId').innerHTML = aColor[x];

The array has three elements in it. 数组中包含三个元素。 Each element contains a string. 每个元素包含一个字符串。 Each string contains a representation of a link for an assorted color. 每个字符串都包含一个代表各种颜色的链接。 The variable 'x' is a random address for the array, (0, 1, 2). 变量“ x”是数组(0,1,2)的随机地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM