javascript - Why href link is not adding to the html using jquery
问题描述
I create the variable for event link, as the user input the link, I want it to display as a hyperlink in HTML by using jquery. Since user could do a infinite input, therefore the output of the link will be on the next line.
var $event_link_selector = $('#event-link');
var event_link = $event_link_selector.val();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label for="event-link">Event Link:</label>
<textarea id="event-link"></textarea>
Ex: If the user enters www.example.com into the test area, I want the page to return me that link and be able to click on it. These are the jquery I currently trying but not working
//returns me the user input link as string instead of link
"<br> <a class='event-description'>"+ event.event_link + "</a>"
//returns none
"<br> <a class='event-description' href='" + event.event_link + "'></a>"
解决方案
You didn't share your whole jQuery code... But it's already clear your quotes were misplaced!
$('#event-link').on('focusout',function(){
var event_link=$(this).val();
$(this).parent('.link-container').append('<a class="event-description" href="' + event_link + '">'+event_link+'</a>');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="link-container">
<label for="event-link">Event Link:</label>
<textarea id="event-link"></textarea>
</div>
推荐阅读
- jenkins-pipeline - 模拟 jenkins.model.Job 的 getLastSuccessfulBuild 结果没有方法签名适用于参数类型
- azure - 无法远程运行 API(Azure 上的 API)
- java - byte[] 在流式传输时不断累积数据,而不只是推送当前数据
- ajax - Ajax 发布请求返回成功但不插入数据库 laravel
- git - 我想将 svn 项目复制到带有历史记录的 git
- python - 如何计算所有可能的列组合并对值求和
- django - 如何强制 redirect_uri 将 HTTPS 与 python-social-app 一起使用?
- excel - 根据单元格数据 B2 的值,单元格 C2:W2 也将设置为数字 0
- python - MacOS 鼠标指针随着信号闪烁和 QLineEdit 闪烁光标
- ckeditor - DNN 中奇怪的 2sxc 内容(CKEditor)