gpt4 book ai didi

javascript - 检测文本区域和输入中的链接

转载 作者:行者123 更新时间:2023-11-28 03:18:26 25 4
gpt4 key购买 nike

我希望允许用户将超链接放入此 textareainput 中,并且可以使用 AngularJS、JavaScript 单击它们:

<div class="col-md-12 form-group">
<div class="col-md-9">
<textarea id="obs" class="sigma-form-simple-search-input large-size" rows="5"
ng-model="commandeHdr.obs" ng-change="hasChanges.parameter=true"/>
</div>
</div>

最佳答案

我使用这种简单的技术为您完成了此操作,我将创建一个空的 Div,然后用从 TextArea 生成的链接填充它,每个“文本链接”之间用分号分隔,

我唯一没有为您做的就是应用一些 CSS 将我们的链接样式设置为文本,我相信您可以做到这一点,这是一项简单的任务。

就像这样 test1.com;test2.com;test3.com,在本例中我们有 3 个网站,但它们处于文本模式,以下是我为您执行此操作的方法:

<div id="container" class="col-md-12 form-group">
<div id="navi" class="col-md-9">
<textarea id="obs" class="sigma-form-simple-search-input large-size"
rows="5" ng-model="commandeHdr.obs"
ng-change="hasChanges.parameter=true" cols="100" rows="10">
test1.com;test2.com;test3.com
</textarea>

<div id="links">
</div>

</div>
</div>

这是我用于执行此任务的 Javascript...

<script>
// get your textarea
var ts = document.getElementById("obs");
// get our empty div
var linksDiv = document.getElementById("links");
// remove any empty space
var urls = ts.value.trim();
// split our links by ;
var urlsArr = urls.split(';');
// get back all new item in original posision, index 3 will be in 1 so I will revese they array...
urlsArr.reverse();
//Cleaned from space....
ts.value = urlsArr;
// our links will be hold here
var links = '';
// for all links we will add
// ID if you need
// Class so we can posision all links by CSS
for (var i in urlsArr) {
links = "<a class='genlinks' id = '"+i+"' href="+urlsArr[i]+">"+urlsArr[i]+";</a>" + links;
}
// now update our div
linksDiv.innerHTML = links;
</script>

你需要的一个简单的CSS是

<style>
#container {
width: 100px;
height: 100px;
position: relative;
}
#links {
width: 100%;
height: 100%;
position: absolute;
top:3px;
left: 18px;
}
#links {
z-index: 5;
}
</style>

如果您的问题没有解决,请告诉我。快乐编码。

这是我使用过的整个 HTML 文件。

<html>  
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<title>awatitwork</title>

<style>
#container {
width: 100px;
height: 100px;
position: relative;
}
#links {
width: 100%;
height: 100%;
position: absolute;
top:3px;
left: 18px;
}
#links {
z-index: 5;
}
</style>
</head>

<body>
<div id="container" class="col-md-12 form-group">
<div id="navi" class="col-md-9">
<textarea id="obs" class="sigma-form-simple-search-input large-size" rows="5" ng-model="commandeHdr.obs" ng-change="hasChanges.parameter=true" cols="100" rows="10">
test1.com;test2.com;test3.com
</textarea>

<div id="links">
</div>

</div>
</div>

<script>
// get your textarea
var ts = document.getElementById("obs");
// get our empty div
var linksDiv = document.getElementById("links");
// remove any empty space
var urls = ts.value.trim();
// split our links by ;
var urlsArr = urls.split(';');
// get back all new item in original posision, index 3 will be in 1 so I will revese they array...
urlsArr.reverse();
//Cleaned from space....
ts.value = urlsArr;
// our links will be hold here
var links = '';
// for all links we will add
// ID if you need
// Class so we can posision all links by CSS
for (var i in urlsArr) {
links = "<a class='genlinks' id = '"+i+"' href="+urlsArr[i]+">"+urlsArr[i]+";</a>" + links;
}
// now update our div
linksDiv.innerHTML = links;
</script>

<!-- Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
</html>

关于javascript - 检测文本区域和输入中的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59422484/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com