使用 PHP 或 Javascript 连接 HTML 字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7280808/
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-24 01:37:00  来源:igfitidea点击:

Concatenation of HTML string using PHP or Javascript

phpjavascripthtmlstringconcatenation

提问by Bruno

I need to do a HTML string concat (using PHP or Javascript) and I don't know how to do it.

我需要做一个 HTML 字符串连接(使用 PHP 或 Javascript),但我不知道该怎么做。

What I'd like

我想要什么

<src="http://maps.google"+$city+"rest_of_the_link">

(I've already tried the previous code but it doesn't do the trick)

(我已经尝试过以前的代码,但它没有成功)

采纳答案by Your Common Sense

Using PHP:

使用 PHP:

<src="http://maps.google<?=$city?>rest_of_the_link">

回答by Quentin

You can't. HTML doesn't have strings … or variables. It is a markup language, not a programming language.

你不能。HTML 没有字符串……或变量。它是一种标记语言,而不是一种编程语言。

You can generate HTML from another language (which could be a program that is executed automatically by a webserver to generate the content for a URL), and you can use DOM APIs (again from another language) to manipulate a DOM generated from an HTML document.

您可以使用另一种语言生成 HTML(可以是由网络服务器自动执行以生成 URL 内容的程序),并且您可以使用 DOM API(同样来自另一种语言)来操作从 HTML 文档生成的 DOM .

回答by Madara's Ghost

You cannot do that with HTML alone. HTML is not a programming language, it's a markup language (hence the HyperText MarkupLanguage), for that reason, no variables (or control structures, or anything of that sort) is in it.

单独使用 HTML 无法做到这一点。HTML 不是一种编程语言,它是一种标记语言(因此是超文本标记语言),因此,其中没有变量(或控制结构,或任何此类)。

What you need to do, is to supply yourself with a different language to help generatethe HTML.

您需要做的是为自己提供一种不同的语言来帮助生成HTML。

For example, in PHP you'd do something like so:

例如,在 PHP 中你会做这样的事情:

<?php
    $city = "London"; //Set the variable
    echo "<src=\"http://maps.google".$city."rest_of_the_link\">"; //Echo result concatenating $city inside.
?>

回答by Uri

I think what you're looking for is templating.

我认为你正在寻找的是模板。

You can load the HTML as a template, and then plant values in specific locations. If you load it from the server side you can use django templates (I use django and my backend is in python), and if you're doing it from the client size, through javascript, you can use jquery templates or the jqote plugin for jquery which is apparently faster than the native jquery solution.

您可以将 HTML 作为模板加载,然后在特定位置植入值。如果你从服务器端加载它,你可以使用 django 模板(我使用 django,我的后端是在 python 中),如果你是从客户端大小,通过 javascript,你可以使用 jquery 模板或 jqote 插件jquery 显然比原生 jquery 解决方案快。

For example, If you're using django, your html would look something like this:

例如,如果您使用 django,您的 html 将如下所示:

<src="http://maps.google{{city}}rest_of_the_link">

And when you load it using the server, you pass a dictionary object that has a value for the key 'city'.

当您使用服务器加载它时,您会传递一个字典对象,该对象具有键“城市”的值。

I'm not sure what is the best method for php templates, but this is something you can probably find easily when you know what you're searching for.

我不确定 php 模板的最佳方法是什么,但是当您知道要搜索的内容时,您可能很容易找到这种方法。

回答by Thomas

<src="maps.google<?php echo $city ?>rest_of_the_link">

Assuming you're using PHP and this is in a .php file.

假设您使用的是 PHP,并且它位于 .php 文件中。

回答by Bas Slagter

You need a serverside language like PHP or ASP to do this. You can also build the url dynamically using Javascript (or a combination of one of these options) but your question does not make clear where the value of the variable must come from.

您需要像 PHP 或 ASP 这样的服务器端语言来执行此操作。您还可以使用 Javascript(或这些选项之一的组合)动态构建 url,但您的问题没有说明变量的值必须来自哪里。

回答by Priyashil

Html is not a programming language, you can achieve it by using JavaScript DOM or JQuery. Just access the the anchor object, manipulate and assign its attribute.

Html 不是编程语言,您可以通过使用 JavaScript DOM 或 JQuery 来实现。只需访问锚点对象,操作并分配其属性即可。