Javascript 如何动态生成文本链接?--JQuery初学者
我在一页上有一堆这样的东西:Javascript 如何动态生成文本链接?--JQuery初学者,javascript,jquery,Javascript,Jquery,我在一页上有一堆这样的东西: <div class="item" id="555"> <div class="wrapper> <p>Make Link One</p> </div> <p class="action">Make Link Two</p> </div> 您只需将单击事件处理程序绑定到包含div,该div使用自己的ID重定向用户: $('.item').on('c
<div class="item" id="555">
<div class="wrapper>
<p>Make Link One</p>
</div>
<p class="action">Make Link Two</p>
</div>
您只需将单击事件处理程序绑定到包含div,该div使用自己的ID重定向用户:
$('.item').on('click', function () {
window.location = 'http://' + this.id;
});
如果容器元素中有其他不应触发重定向的内容,则可以绑定到容器中的
元素:
$('.item').on('click', 'p', function () {
window.location = 'http://' + $(this).parents('.item:first')[0].id;
});
顺便说一句,ID不应该以数字开头。以下是指向IDs正确语法的链接:
请注意,.on()
在jQuery 1.7中是新的。在第一个示例中,将替换已折旧的.bind()
,在第二个示例中,将替换已折旧的.delegate()
您只需将单击事件处理程序绑定到包含的div,该div使用其自己的ID重定向用户:
$('.item').on('click', function () {
window.location = 'http://' + this.id;
});
如果容器元素中有其他不应触发重定向的内容,则可以绑定到容器中的
元素:
$('.item').on('click', 'p', function () {
window.location = 'http://' + $(this).parents('.item:first')[0].id;
});
顺便说一句,ID不应该以数字开头。以下是指向IDs正确语法的链接:
请注意,.on()
在jQuery 1.7中是新的。在第一个示例中,它替换了已折旧的.bind()
,在第二个示例中,它替换了已折旧的.delegate()
$('p')。每个(函数(){
var$this=$(this);
var href=$this.parents('.item:first')[0].id;
$this.wrap(“”);
});
。每个
循环通过找到的p
元素。然后,它通过类.item
($this.parents('.item:first')
)查找父级IW以获取id。[0]
部分将jQuery对象转换为一个标准DOM元素,以便我们可以轻松地从该元素中获取id
属性(您也可以通过$this.parents('.item:first').attr('id'))
坚持使用纯jQuery)。最后,我们用正确的href
$('p')将p
包装在锚定标记中{
$(document).ready(function(){
$(".item p").each(function(){
var t1 = $(this).text();
var linkid = $(this).parents('.item:first').attr("id");
$(this).parent().append('<a href="http://' + linkid + '">' + t1 + '</a>');
$(this).remove();
});
});
var$this=$(this);
var href=$this.parents('.item:first')[0].id;
$this.wrap(“”);
});
。每个
循环通过找到的p
元素。然后,它通过类.item
($this.parents('.item:first')
)查找父级IW以获取id。[0]
部分将jQuery对象转换为一个标准DOM元素,以便我们可以轻松地从该元素中获取id
属性(您也可以通过$this.parents('.item:first').attr('id'))
坚持使用纯jQuery)。最后,我们用正确的href
$(document.ready(function())将p
包装在锚定标记中{
$(document).ready(function(){
$(".item p").each(function(){
var t1 = $(this).text();
var linkid = $(this).parents('.item:first').attr("id");
$(this).parent().append('<a href="http://' + linkid + '">' + t1 + '</a>');
$(this).remove();
});
});
$(“.p项”)。每个(函数(){
var t1=$(this.text();
var linkid=$(this).parents('.item:first').attr(“id”);
$(this.parent().append(“”);
$(this.remove();
});
});
$(文档).ready(函数(){
$(“.p项”)。每个(函数(){
var t1=$(this.text();
var linkid=$(this).parents('.item:first').attr(“id”);
$(this.parent().append(“”);
$(this.remove();
});
});
我会这样做,假设您希望它应用于每个p
,它是具有项的元素的子元素
类:
$(function(){
$('.item p').each(function(){
var $this = $(this);
$this.contents().wrap($('<a>', { href: $this.closest('.item').attr('id') }));
});
});
$(函数(){
$('.item p')。每个(函数(){
var$this=$(this);
$this.contents().wrap($('',{href:$this.closest('.item').attr('id')}));
});
});
我会这样做,假设您希望它应用于每个p
,它是具有项的元素的子元素
类:
$(function(){
$('.item p').each(function(){
var $this = $(this);
$this.contents().wrap($('<a>', { href: $this.closest('.item').attr('id') }));
});
});
$(函数(){
$('.item p')。每个(函数(){
var$this=$(this);
$this.contents().wrap($('',{href:$this.closest('.item').attr('id')}));
});
});
是否有办法在悬停时设置样式,使其看起来像普通链接?(即显示光标、下划线等)可以通过一些CSS完成<代码>.p项:悬停{文本装饰:下划线;}
。您可以使用JavaScript,但我建议使用CSS:$('.item').find('p').on('mouseenter',function(){$(this).CSS({'text-decoration':'underline'})on('mouseleave',function(){$(this).CSS({'text-decoration':'none'});})代码>是否有办法在悬停时设置样式,使其看起来像普通链接?(即显示光标、下划线等)可以通过一些CSS完成<代码>.p项:悬停{文本装饰:下划线;}
。您可以使用JavaScript,但我建议使用CSS:$('.item').find('p').on('mouseenter',function(){$(this).CSS({'text-decoration':'underline'})on('mouseleave',function(){$(this).CSS({'text-decoration':'none'});})
如果使用.parents('.item:first')
可以在任何级别支持标记。如果使用.parents('.item:first')
可以在任何级别支持
标记。