简体   繁体   English

如何在 html 的输入文本字段中创建超链接?

[英]How do I create a hyperlink inside an input text field in html?

I have an input text field in which the user can enter the name of a website.我有一个输入文本字段,用户可以在其中输入网站的名称。 Is it possible for it to be hyperlinked so that once the field is saved, if the user clicks on it, it redirects to the website?是否可以对其进行超链接,以便在保存该字段后,如果用户单击它,它会重定向到该网站?

I use Django for the backend and Javascript and html for the front end.我使用 Django 作为后端,使用 Javascript 和 html 作为前端。

An input field's value is stored as plain text, therefore you cannot include HTML (ie a link) and expect the HTML to be parsed and functioning.输入字段的值存储为纯文本,因此您不能包含 HTML(即链接)并期望 HTML 被解析和运行。

You could simulate this behavior with JavaScript, however I would recommend against it.您可以使用 JavaScript 模拟这种行为,但我建议您不要这样做。 (You would add a click listener, your function would pull the value of the field, see if it is a valid URL, and then open up the location.) (您将添加一个点击侦听器,您的函数将拉取该字段的值,查看它是否是一个有效的 URL,然后打开该位置。)

I'm not going to write the code for this because it would be a terrible user experience.我不打算为此编写代码,因为这将是一个糟糕的用户体验。 The standard behavior for an input field is that you click on it to edit the text.输入字段的标准行为是单击它来编辑文本。 This is an assumption your users have, and they would therefore (a) not think to click on it because they don't expect it to be a link, and (b) click in it if they wanted to edit the text, only to be redirected and unable to edit the text.这是您的用户的假设,因此他们 (a) 不会考虑点击它,因为他们不希望它是一个链接,并且 (b) 如果他们想编辑文本,则点击它,只是为了被重定向并且无法编辑文本。

Alternatively, you could add a small button next to the input, ie 'Open' or 'Test' or an external link icon.或者,您可以在输入旁边添加一个小按钮,即“打开”或“测试”或外部链接图标。

Simply, if you are printing the URL for the user on their profile page, sure you can just print it as follows:简单地说,如果您要在用户的个人资料页面上打印用户的 URL,请确保您可以按如下方式打印它:

<a href='$url'>$url</a>

That's PHP but of course you can do this in any language.那是 PHP,但当然你可以用任何语言来做到这一点。 I'm not sure if this answers your question since you ask if it can be printed "inside" the field which isn't possible/doesn't make sense.我不确定这是否能回答你的问题,因为你问它是否可以在不可能/没有意义的字段“内部”打印。

HOWEVER, think about security.但是,请考虑安全性。 Remember the user can enter any malicious URL into this field, so you need to be aware of who you are potentially linking this to on your website/application.请记住,用户可以在此字段中输入任何恶意 URL,因此您需要了解您可能将其链接到您的网站/应用程序上的谁。

Useful resources:有用的资源:

URL HTML field: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/url URL HTML 字段: https : //developer.mozilla.org/en-US/docs/Web/HTML/Element/input/url

Validate URL format with Django (does not check if malicious or not though): How can I check if a URL exists with Django's validators?使用 Django 验证 URL 格式(尽管不检查是否恶意): 如何使用 Django 的验证器检查 URL 是否存在?

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

相关问题 我如何在此文本中创建超链接 - how do i create the hyperlink in this text 如何在 html 的输入字段中突出显示新文本 - How do I highlight new text within a input field in html 如何在输入类型文本(文本字段)中创建表? - How to create table inside an Input type text (text field)? 如何基于 html select 选项填充 html 文本输入字段 - How do I populate an html text input field based on an html select option 如何在javascript中将输入字段动态添加到我的html表格行内的单元格中? - How do I dynamically add an input field into a cell inside my html table row in javascript? 如何将来自HTML文本字段的输入永久存储到jQuery变量中? - How do I permanently store input from an HTML text field into a jQuery variable? 如何正确地将 HTML 输入字段中的文本链接到 JavaScript - How do I properly link text from an input field in HTML to JavaScript 如何使用prototype.js在html表单的文本字段内获取值? - How do I take value inside a text field of a html form using prototype.js? 如何创建一个 html 输入字段,它将自动填充 totp 验证码 - How do I create an html input field which will auto fill totp verification codes 如何像 iPhone 一样在 HTML 文本输入框中放置一个清除按钮? - How do I put a clear button inside my HTML text input box like the iPhone does?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM