在客户端连接 HTML 中的两个字符串(无服务器)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18828678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 13:28:49  来源:igfitidea点击:

Concatenate Two strings in HTML in client side(No Server)

javascripthtml

提问by Rajesh Paul

Can I concatenate two strings in HTML?

我可以在 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.

它可以document.write()在 javascript 中使用完成,但我想知道HTML 本身是否有任何连接功能

回答by simonzack

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

不,没有。HTML 是标记,它不是图灵完备的。

回答by CBroe

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

用 JavaScript 实现这一点的一种(原始)方法是

<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 generatedby JS in the first place.

但是由于当 JS 不可用时这些链接是无用的,所以它们应该首先由 JS生成

回答by Scription

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

不,没有。HTML 是标记。您应该使用动态 HTML 和 JavaScript 来实现这一点。

回答by Madan Ram

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

你可以通过在 java 脚本中使用 this.href 来做到这一点

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

回答by David

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:

这不能以您尝试的方式完成,但是如果 JavaScript 无论如何都在客户端上运行,那么您仍然可以实现您正在寻找的功能。您只需要将标签与脚本分开:

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

回答by user3709977

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>

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