admin管理员组

文章数量:1400455

Here is my code

var test = '${URL}'

${URL} is an anchor tag containing the follow <a href="google">LINK</a>

I want to change this LINK to TEST. Is it possible to do that? I couldn't find any method to just change the innerHTML of the anchor tag.

Here is my code

var test = '${URL}'

${URL} is an anchor tag containing the follow <a href="google.">LINK</a>

I want to change this LINK to TEST. Is it possible to do that? I couldn't find any method to just change the innerHTML of the anchor tag.

Share Improve this question edited Jan 25, 2014 at 2:25 Antony 15.1k10 gold badges47 silver badges76 bronze badges asked Jan 25, 2014 at 2:17 CamelCaseCamelCase 2333 gold badges6 silver badges17 bronze badges 2
  • 1 '${URL}' is not JavaScript nor HTML. I'm not sure if this is any language related to JS, but if it is, please include that in the tags to your question. Also, try doing test.src = 'http://google.' to change your target url to http://google.. Is that what you're looking for? – Joeytje50 Commented Jan 25, 2014 at 2:21
  • looks like some template replacement code – Patrick Evans Commented Jan 25, 2014 at 2:27
Add a ment  | 

2 Answers 2

Reset to default 4

First give an id to the html link. Then you can access it and update the text by using innerHTML property as below example.

<html>
<head>
</head>
<body>
<a href="google." id='link'>LINK</a>

<script>
    document.getElementById('link').innerHTML="Test";
</script>
</body>
</html>
  • Reference: http://www.w3schools./jsref/prop_html_innerhtml.asp

The variable 'test' does not hold a DOM object. All that is to it at that point is a String that happens to look like html.

If you did this with the test variable.

var div = document.createElement('div');
div.innerHTML += test;

div.getElementsByTagName("a")[0].innerHTML = 'test';

You could do it. But at that current stage in your code, the variable test is no more that a String.

If you could create a better picture of the environment and what you want to happen we could help you a lot better.

本文标签: Set innerHTML of anchor in JavaScriptStack Overflow