gpt4 book ai didi

javascript - 如何使用 JavaScript 和 CSS 从循环文本创建链接?

转载 作者:太空宇宙 更新时间:2023-11-04 03:11:44 25 4
gpt4 key购买 nike

我正在使用 jQuery 和 CircleType.js以圆形图案显示文本。我的问题是如何使圆圈中的文字成为超链接? circletype.js 会自动删除所有使用的 a 元素,并且由于此方法在每个字母周围放置了一个 span 标记,所以我不确定如何将 circletype.js 与链接一起使用。

有没有另一种方法可以在不使用 circletype.js 的情况下将文本包装成带有链接的圆形?我仍然是 JavaScript 的新手,所以如果有一种方法可以使用 JS 创建链接,那么任何帮助走上正确的道路来为此编写脚本的人都会很棒。我的代码如下(减去 css):

<body>
<div id="img-container">
<p id="link-circle">
LINK &diams; LINK &diams; LINK &diams; LINK &diams; LINK &diams;&nbsp;
</p>
<a href="#">
<div id="switch">
<form action="">
<input type="radio" id="left-radio" name="Column Select" value="left"/>
<input type="radio" id="right-radio" name="Column Select" value="right"/>
</form>
<span id="dbl-chevron">&raquo;</span>
</div>
</a>
</div>
<script type="text/javascript">
$( document ).ready(function() {
$('#link-circle').circleType();
});
</script>
</body>

这是一个例子:

Example

也许甚至可以在文本上叠加一个 div 层来创建链接。但我不确定该采用哪种方式。在此先感谢您的帮助!

最佳答案

看了the plugin's code之后和它一起玩,我发现它是必需的 LetteringJS正在删除任何标签的插件。

他们在第 33 行调用它:$(elem).lettering();

然后我想出了一个主意。

我们能做什么?

  • 我们可以在元素被 LetteringJS 拆分之前保存我们元素的 HTML .
  • 然后,让它完成它的工作。
  • 之后,我们可以将标签放回包含在其中的每个字母周围。

部分解决方案

为什么是“部分”?

我的解决方案只适用于:

  1. 非自闭标签:<img/> , <input/>等,但我认为这不太可能发生。
  2. 非嵌套标签:<b><a></a></b> ,但你可以用类伪造它 - 请参见下面的演示。

它是如何工作的?

我们需要改变CircleType的源代码,以便它做我们想要的。我已经评论了我的代码,但如果您不明白什么,或者发现错误或需要改进,请不要犹豫告诉我!

// trim spaces at the beginning and at the end
elem.innerHTML = elem.innerHTML.replace(/^\s+|\s+$/g, '');

// grab the HTML string
var temp = elem.innerHTML;

// replace any space that is not part of a tag with a non-breakable space (&nbsp;)
elem.innerHTML = elem.innerHTML.replace(/<[^<>]+>|\s/g, function(s) {
return s[0] == '<' ? s : '&nbsp;';
});

// wrap each character in a span
$(elem).lettering();


var inTag = false, // are we between tags? (<i>here</i>)
isTag = false, // are we inside a tag? (<here></here>)
tagNum = -1, // how many opening tags have we met so far? (minus 1)
pos = 0, // character position (excluding tags)
dom = document.createElement('div'); // temporary dom

dom.innerHTML = temp; // clone our element in the temporary dom

var tags = dom.children; // children of the element
// for each of them, empty their content
for(var i=0, l=tags.length; i<l; i++){
tags[i].innerHTML = '';
}

// for each character in our HTML string
for(var i=0, l= temp.length; i<l; i++){
var c = temp[i];
// if it's a '<'
if(c == '<'){
// and if it's an opening tag
if(!inTag){
// increment the number of tags met
tagNum++;
// we're in a tag!
inTag = true;
}
else{
// otherwise we're in a closing tag
inTag = false;
}
// we're on a tag (<here>)
isTag = true;
}
// if it's a '>'
else if(c == '>'){
// we're not <here> anymore
isTag = false;
}
// if we're not <here>
else if(!isTag){
// if we're <b>here</b>
if(inTag){
// replace the span's content with our tag
elem.children[pos].innerHTML = tags[tagNum].outerHTML;
// put the letter back in
elem.children[pos].children[0].innerHTML = c;
}
// move forward in the spans
pos++;
}
}

资源

关于javascript - 如何使用 JavaScript 和 CSS 从循环文本创建链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29399425/

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