jQuery-从Element&获取文本添加到属性 [英] jQuery - Get Text From Element & Add To Attribute

查看:65
本文介绍了jQuery-从Element&获取文本添加到属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍在学习jQuery-很抱歉,我问的很多,或者我问的是简单还是愚蠢.

I'm still learning jQuery - So I'm sorry if I'm asking a lot, or if what I'm asking is easy or silly.

当我有一个问题时,我通常会尝试举一个例子说明我尝试过的事情,但老实说,我不知道从哪里开始!因此,即使是关于我需要做的一些指导,也还可以!我很愿意尝试自己做这个,但我不确定对Google有什么帮助!

When I have a question I usually try to give an example of what I've tried but I honestly don't know where to start! So even some guidance on what I need to do would be more than okay! I'm more than willing to try and do this myself, I'm just not sure what to Google!

基本上,我想做的是获取锚点的内容并将其添加到跟踪属性.然后将属性中的空格交换为+的

Basically what I want to do is get content of my Anchors and add them to a tracking attribute. And then the spaces in the attribute be swapped to +'s

代码:

<a href="#" tracking="Banner-_-Link+1-_-">LINK ONE</a>
<a href="#" tracking="Banner-_-Link+2-_-">LINK TWO</a>

加载后代码

<a href="#" tracking="Banner-_-Link+1-_-LINK+ONE">LINK ONE</a>
<a href="#" tracking="Banner-_-Link+2-_-LINK+TWO">LINK TWO</a>

任何指导将不胜感激!

谢谢!

推荐答案

一个接一个:

获取具有tracking属性的所有链接:

Get all links with a tracking attribute:

var anchors = $('a[tracking]');

并遍历它们:

anchors.each( function() {

在每个点上,获取a元素及其文本(将' '替换为'+'):

at each point, get the a element and its text (replacing ' ' with '+'):

  var a = $(this); 
  var txt = a.text().replace(' ', '+');

并将该文本附加到现有属性:

and append that text to the existing attribute:

  a.attr('tracking', a.attr('tracking') + txt);

我们完成了.

});

示例: http://codepen.io/paulroub/pen/Extrl

这篇关于jQuery-从Element&amp;获取文本添加到属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆