使用 JavaScript (jQuery),我如何循环遍历所有“a”?页面上的标签,一次一个,并为每个标签分配单独的样式?

发布于 2024-09-25 06:58:21 字数 1433 浏览 0 评论 0原文

让我尝试解释一下...假设我在页面上有以下“a”标签:

<a href="img1.jpg" class="myClass" alt="0,0,600,200"></a>
<a href="img2.jpg" class="myClass" alt="200,0,600,75"></a>
<a href="img3.jpg" class="myClass" alt="275,0,600,200"></a>
<a href="img4.jpg" class="myClass" alt="475,0,600,50"></a>
<a href="img5.jpg" class="myClass" alt="525,0,600,100"></a>
<a href="img6.jpg" class="myClass" alt="625,0,600,200"></a>

并且我想循环遍历具有“myClass”类的所有“a”标签并读取 alt 标签并根据其值分配特定的值“风格”属性。

我尝试了以下操作:

// get value of alt attribute
var tmp = $("a.myClass").attr('alt');
// split value into an array
var partsArray = tmp.split(',');
// assign array values to a dedicated variable
var a = partsArray[0];
var b = partsArray[1];
var c = partsArray[2];
var d = partsArray[3];

// create inline style string
var style;
style = style + "position:absolute;display:block;";
style = style + "border:1px solid red;";
style = style + "width:" + c + "px;";
style = style + "height:" + d + "px;";
style = style + "top:" + a + "px;";
style = style + "left:" + b + "px;";

// add the style attribute and its value
$('a.myClass').attr('style', style);

但它采用第一个“a”标签“alt”属性的值并将样式分配给所有“a”标签。我想做的是读取每个“a”标签,获取“alt”值并为该“a”标签分配样式,然后读取下一个“a”标签...

有没有一种方法可以完成此操作而不需要必须为每个“a”标签分配唯一的 ID 或类?
我对 jQuery 完全陌生,花了几个小时尝试和谷歌搜索这个问题,但不知道如何解决。
请有人帮忙。

Let me try to explain... Lets say I have the following 'a' tags on a page:

<a href="img1.jpg" class="myClass" alt="0,0,600,200"></a>
<a href="img2.jpg" class="myClass" alt="200,0,600,75"></a>
<a href="img3.jpg" class="myClass" alt="275,0,600,200"></a>
<a href="img4.jpg" class="myClass" alt="475,0,600,50"></a>
<a href="img5.jpg" class="myClass" alt="525,0,600,100"></a>
<a href="img6.jpg" class="myClass" alt="625,0,600,200"></a>

and I want to cycle through all 'a' tags with class 'myClass' and read the alt tag and based on its value assign specific 'style' attributes.

I tried the following:

// get value of alt attribute
var tmp = $("a.myClass").attr('alt');
// split value into an array
var partsArray = tmp.split(',');
// assign array values to a dedicated variable
var a = partsArray[0];
var b = partsArray[1];
var c = partsArray[2];
var d = partsArray[3];

// create inline style string
var style;
style = style + "position:absolute;display:block;";
style = style + "border:1px solid red;";
style = style + "width:" + c + "px;";
style = style + "height:" + d + "px;";
style = style + "top:" + a + "px;";
style = style + "left:" + b + "px;";

// add the style attribute and its value
$('a.myClass').attr('style', style);

but it takes the value of the first 'a' tag 'alt' attribute and assigns the style to all 'a' tags. What I am trying to do is read each 'a' tag, get the 'alt' value and assign a style to THAT 'a' tag only THEN read the next 'a' tag ...

Is there a way to accomplish this WITHOUT having to assign a unique ID or class to each 'a' tag?
I am totally new to jQuery and have spent hours trying and googleing this issue and can't figure out how.
Someone PLEASE help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

颜漓半夏 2024-10-02 06:58:21

您可以使用 each 方法,如下所示:

$('a.myClass').each(function(){
  var tmp = $(this).attr('alt');

  // your further code...
});

every 将迭代 myClass 类的所有链接,并获取存储在您可以操作的 tmp 变量中的每个链接的 alt 值。

You can use the each method like this:

$('a.myClass').each(function(){
  var tmp = $(this).attr('alt');

  // your further code...
});

The each will iterate over all links with class myClass and get each link's alt value stored in tmp variable you can act upon.

岁月苍老的讽刺 2024-10-02 06:58:21

事实上,你离你需要去的地方并不太远。

// get value of alt attribute
$("a.myClass").each(function() {
  var tmp = $(this);

  // split value into an array
  var partsArray = tmp.attr("alt").split(',');

  // assign array values to a dedicated variable
  var t = partsArray[0];
  var l = partsArray[1];
  var w = partsArray[2];
  var h = partsArray[3];

  tmp.css({
    "position" : "absolute",
    "display" : "block",
    "border" : "1px solid red",
    "width" : w,
    "height" : h,
    "top" : t,
    "left" : l
  });
});

You're actually not too far from where you need to be.

// get value of alt attribute
$("a.myClass").each(function() {
  var tmp = $(this);

  // split value into an array
  var partsArray = tmp.attr("alt").split(',');

  // assign array values to a dedicated variable
  var t = partsArray[0];
  var l = partsArray[1];
  var w = partsArray[2];
  var h = partsArray[3];

  tmp.css({
    "position" : "absolute",
    "display" : "block",
    "border" : "1px solid red",
    "width" : w,
    "height" : h,
    "top" : t,
    "left" : l
  });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文