6

Can I concatenate two strings in HTML?

I want to achieve the following functionality-

<a href="#"+"javascript:document.getElementsByTagName('div')[0].id">go to the 1st DIV tag.</a>

It could have been done using document.write() in javascript but I want to know if there is any concatenation functionality in HTML itself.

4

6 回答 6

14

No, there isn't. HTML is markup, it is not turing complete.

于 2013-09-16T12:59:49.163 回答
4

One (primitive) way to achieve this with JavaScript would be

<a href="#"
  onclick="window.location.hash='#'+document.getElementsByTagName('div')[0].id; return false;">
  go to the 1st DIV tag.
</a>

But since those links are useless when JS is not available, they should probably only be generated by JS in the first place.

于 2013-09-16T13:03:57.337 回答
2

No, there isn't. HTML is markup. You should use dynamic HTML and JavaScript to achieve this.

于 2013-09-16T13:02:52.117 回答
2

you can do this by using this.href in java script

<a href="#" onload="this.href=this.href+document.getElementsByTagName('div')[0].id;" >

ex

<a href="targetWithInDoc.html" onload="this.href=this.href+'#block1';" >block 1</a>
于 2013-09-16T13:03:10.940 回答
1

This can't be done in the way you're attempting, but if JavaScript is running on the client anyway then you can still achieve the functionality you're looking for. You just need to separate the tag from the script:

<a href="#" id="someID">Go to the first DIV tag</a>

<script type="text/javascript">
    document.getElementById('someID').href = '#' + document.getElementsByTagName('div')[0].id;
</script>
于 2013-09-16T13:03:23.350 回答
0

I know it wont help u now but I'm posting this for others who will come to this question by searching

we can achieve it this way :

<a href='<%#String.Concat("string1", "string2")%>'></a>

于 2015-07-23T07:25:24.080 回答