使 HTML 'value' 属性成为链接

我想将 HTML 输入值的输出转换为可点击的链接。

目前,它看起来像:<input type="email" class="form-control" name="contactEmail" value="<?php echo $row_rsContactDetails['contactEmail']; ?>">

我尝试使用 PHP 和 JavaScript 来创建链接,但这最终只是逐字显示 HTML 代码。

可以吗?如果可以,怎么做?


万千封印
浏览 154回答 3
3回答

紫衣仙女

您不需要输入作为值输出的链接,您需要一个解决方法,这是我的建议:https://jsfiddle.net/4w58ed3o/1/HTML:<div class="box">&nbsp; <input type="email" id="myinput" value="default@email.com">&nbsp; <a href="#" id="mylink"></a></div><button id="myswitcher">Edit</button>JS:let input = document.querySelector('#myinput');let link = document.querySelector('#mylink');let myswitcher = document.querySelector('#myswitcher');let setLink = () => {&nbsp; &nbsp; link.innerHTML = input.value;&nbsp; link.setAttribute('href', 'mailto:' + input.value);}input.addEventListener('input', () => {&nbsp; &nbsp; setLink();});setLink();myswitcher.addEventListener('click', () => {&nbsp; &nbsp; document.querySelector('.box').classList.add('editable');&nbsp; input.focus();});input.addEventListener('blur', () => {&nbsp; &nbsp; document.querySelector('.box').classList.remove('editable');});CSSinput, a {&nbsp; display: inline-block;&nbsp; padding: 0;&nbsp; padding: 10px;&nbsp; margin: 0;&nbsp; text-decoration: none;&nbsp; font-size: 16px;&nbsp; font-family: inherit;&nbsp; box-shadow: none;&nbsp; border: none;&nbsp;&nbsp;&nbsp; line-height: 1.2;&nbsp; background-color: transparent;}input:focus, a:focus {&nbsp; outline: 1px solid #000;}.box {&nbsp; position: relative;&nbsp; display: inline-block;}input {&nbsp; position: absolute;&nbsp; box-sizing: border-box;&nbsp; top: 0;&nbsp; left: 0;&nbsp; width: 100%;}.box input {&nbsp; opacity: 0;&nbsp; pointer-events: none;}.box.editable a {&nbsp; opacity: 0;&nbsp; pointer-events: none;}.box.editable input {&nbsp; opacity: 1;&nbsp; pointer-events: auto;}我为您制作了一个可行的实施示例,替换来自服务器的数据并受到我的解决方案的启发。

Qyouu

您可以使用 javascript/jquery 来实现相同的目的。<html><body><input type="email" class="form-control" name="contactEmail" value="LINK HERE"><span id="output"></span><script>&nbsp; &nbsp;$(document).ready(function(){&nbsp; &nbsp; &nbsp;var str = "LINK TITLE HERE";&nbsp; &nbsp; &nbsp;var valueToLink = $(".form-control").val() //fetches the string to be converted to&nbsp;&nbsp; &nbsp; //link&nbsp; &nbsp; &nbsp;var result = str.link(valueToLink);&nbsp; &nbsp; &nbsp;document.getElementById("output").innerHTML = result;&nbsp; &nbsp;})</script></body></html>您还可以将上述逻辑连接到事件调用,例如按钮单击。

猛跑小猪

我认为这是不可能的,但你可以尝试一下。您<?php echo $row_rsContactDetails['contactEmail']; ?>也可以放置“一些文本”。<a&nbsp;href="<?php&nbsp;echo&nbsp;$row_rsContactDetails['contactEmail'];&nbsp;?>">SOME&nbsp;TEXT</a>
打开App,查看更多内容
随时随地看视频慕课网APP