如何使用 jQuery 多次更改图像的 alt 属性?

发布于 2024-07-18 06:50:08 字数 983 浏览 13 评论 0原文

这个问题完成了我在这里问的第一个问题 关于如何使用 jQuery 在单击时更改图像。

我忘了说(即使我后来编辑了我的帖子),我正在寻找一种方法,以便每次通过单击更改图像时都具有不同的 alt 属性。

(这是为了更好的可访问性和 SEO 优化。)

以下是实际的 html 代码,感谢 altCognito:

<img id="radio_btn1" src="originalimage1.jpg" />
<br />
  <input type="radio" name="radio_btn1" value='image1.jpg' />
  <input type="radio" name="radio_btn1" value='image2.gif' />
  <input type="radio" name="radio_btn1" value='image3.png' />
  <input type="radio" name="radio_btn1" value='image4.jpeg' />

和 jquery:

imgFldr = 'images/nameofthesubfolder/';
$("input[type='radio']").click(function() {
   $('#'+this.name).attr('src', imgFldr+this.value).attr('alt', 'newattribute');
});

它可以在 jsbin

This question completes the first one that I asked here about how to change an image on click using jQuery.

I forgot to say (even if I edited my post later) that I'm looking for a way to have a different alt attribute each time an image is changed by click.

(This is for better accessibility and SEO optimization.)

Here is the actual html code thanks to altCognito:

<img id="radio_btn1" src="originalimage1.jpg" />
<br />
  <input type="radio" name="radio_btn1" value='image1.jpg' />
  <input type="radio" name="radio_btn1" value='image2.gif' />
  <input type="radio" name="radio_btn1" value='image3.png' />
  <input type="radio" name="radio_btn1" value='image4.jpeg' />

And the jquery:

imgFldr = 'images/nameofthesubfolder/';
$("input[type='radio']").click(function() {
   $('#'+this.name).attr('src', imgFldr+this.value).attr('alt', 'newattribute');
});

It can be edited at jsbin.

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

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

发布评论

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

评论(5

执手闯天涯 2024-07-25 06:50:08

嗯... SEO 和 Javascript 操纵的内容一开始就不应该出现在同一篇文章中。 无论您做什么,如果您使用 Javascript 来显示您的内容,您都会损害您的搜索引擎可见性。

也就是说,您可以将这些图像放在哈希数组中(imagename->alt)并从那里获取 alt,或者您可以将 alt 放入带有像 | 这样的分隔符连接到文件名的单选值中。 然后在用于显示图像的函数中解析它们。

...简而言之:

<img id="radio_btn1" src="originalimage1.jpg" />
<br />
  <input type="radio" name="radio_btn1" value='image1.jpg|Image 1' />
  <input type="radio" name="radio_btn1" value='image2.gif|Image 2' />
  <input type="radio" name="radio_btn1" value='image3.png|Image 3' />
  <input type="radio" name="radio_btn1" value='image4.jpeg|Image 4' />

+

imgFldr = 'images/nameofthesubfolder/';
$("input[type='radio']").click(function() {
   var strarr = this.value.split('|');
   if(strarr.length < 2) return;
   $('#'+this.name).attr('src', imgFldr+strarr[0]).attr('alt', strarr[1]);
});

...或:

<img id="radio_btn1" src="originalimage1.jpg" />
<br />
<input type="radio" name="radio_btn1" value='image1.jpg' />
<input type="radio" name="radio_btn1" value='image2.gif' />
<input type="radio" name="radio_btn1" value='image3.png' />
<input type="radio" name="radio_btn1" value='image4.jpeg' />

+

imgFldr = 'images/nameofthesubfolder/';
var imagesarr = {'image1.jpg': 'Image 1', 'image2.gif': 'Image 2','image3.png': 'Image 3','image4.jpeg': 'Image 4'}
$("input[type='radio']").click(function() {
   $('#'+this.name).attr('src', imgFldr+this.value).attr('alt', imagesarr[this.value]);
});

Well... SEO and content manipulated by Javascript shouldn't be in the same post to begin with. Whatever you do, if you use Javascript to display your content, you're hurting your search engine visibility.

That said, you could either have those images in a hasharray (imagename->alt) and get the alt from there, or you could put the alt in the value of the radio concatenated to the filename with a separator like | and then parse them out in the function you use to display the image.

...so in short either:

<img id="radio_btn1" src="originalimage1.jpg" />
<br />
  <input type="radio" name="radio_btn1" value='image1.jpg|Image 1' />
  <input type="radio" name="radio_btn1" value='image2.gif|Image 2' />
  <input type="radio" name="radio_btn1" value='image3.png|Image 3' />
  <input type="radio" name="radio_btn1" value='image4.jpeg|Image 4' />

+

imgFldr = 'images/nameofthesubfolder/';
$("input[type='radio']").click(function() {
   var strarr = this.value.split('|');
   if(strarr.length < 2) return;
   $('#'+this.name).attr('src', imgFldr+strarr[0]).attr('alt', strarr[1]);
});

...or:

<img id="radio_btn1" src="originalimage1.jpg" />
<br />
<input type="radio" name="radio_btn1" value='image1.jpg' />
<input type="radio" name="radio_btn1" value='image2.gif' />
<input type="radio" name="radio_btn1" value='image3.png' />
<input type="radio" name="radio_btn1" value='image4.jpeg' />

+

imgFldr = 'images/nameofthesubfolder/';
var imagesarr = {'image1.jpg': 'Image 1', 'image2.gif': 'Image 2','image3.png': 'Image 3','image4.jpeg': 'Image 4'}
$("input[type='radio']").click(function() {
   $('#'+this.name).attr('src', imgFldr+this.value).attr('alt', imagesarr[this.value]);
});
轮廓§ 2024-07-25 06:50:08

为什么不将 alt 值放入输入的 rel 中,然后将其也放在图像上:

<img id="radio_btn1" src="originalimage1.jpg" />
<br />
  <input type="radio" name="radio_btn1" value='image1.jpg' rel="alt text 1" />
  <input type="radio" name="radio_btn1" value='image2.gif' rel="alt text 1" />
  <input type="radio" name="radio_btn1" value='image3.png'  rel="alt text 1" />
  <input type="radio" name="radio_btn1" value='image4.jpeg' rel="alt text 1" />
And the jquery:

imgFldr = 'images/nameofthesubfolder/';
$("input[type='radio']").click(function() {
   $("#radio_btn1").attr("src", imgFldr + $(this).val()).attr("alt", $(this).attr("rel"));
});

why not put the alt value in the rel of the input then place that on the image too:

<img id="radio_btn1" src="originalimage1.jpg" />
<br />
  <input type="radio" name="radio_btn1" value='image1.jpg' rel="alt text 1" />
  <input type="radio" name="radio_btn1" value='image2.gif' rel="alt text 1" />
  <input type="radio" name="radio_btn1" value='image3.png'  rel="alt text 1" />
  <input type="radio" name="radio_btn1" value='image4.jpeg' rel="alt text 1" />
And the jquery:

imgFldr = 'images/nameofthesubfolder/';
$("input[type='radio']").click(function() {
   $("#radio_btn1").attr("src", imgFldr + $(this).val()).attr("alt", $(this).attr("rel"));
});
秋日私语 2024-07-25 06:50:08

我不会为此使用单选按钮中的 name 属性。 定义一个自定义属性:data-Img 或类似的东西。 这也将帮助您符合下一个 html 标准。

var imgFldr = 'images/nameofthesubfolder/';

$("input[type='radio']").click(function() {

   var img = '#'+ $(this).attr('data-Img');

   var url = imgFldr + $(this).attr('value');

   $(img).attr('src', url)
            .attr('alt', 'newattribute');

});

你可能可以用一行来做这件事......但我真的不明白这一点。 首先使其可读。

I would not use the name attribute in the radio buttons for this. Define a custom attribute: data-Img or something like that. That would also help you conform to the next html standard.

var imgFldr = 'images/nameofthesubfolder/';

$("input[type='radio']").click(function() {

   var img = '#'+ $(this).attr('data-Img');

   var url = imgFldr + $(this).attr('value');

   $(img).attr('src', url)
            .attr('alt', 'newattribute');

});

You could probably do this in one line...but I don't see the point really. Make it readable first.

溺渁∝ 2024-07-25 06:50:08

你可以尝试这样的事情:

<img id="radio_btn1" src="originalimage1.jpg" />
<br />
<input type="radio" name="radio_btn1" value='image1' />
<input type="radio" name="radio_btn1" value='image2' />
<input type="radio" name="radio_btn1" value='image3' />
<input type="radio" name="radio_btn1" value='image4' />

imgFldr = 'images/nameofthesubfolder/';
var images = {
 image1: {'img': 'image1.jpg', 'alt': 'Alt for img1'},
 image2: {'img': 'image2.gif', 'alt': 'Alt for img2'},
 image3: {'img': 'image3.png', 'alt': 'Alt for img3'},
 image4: {'img': 'image4.jpeg', 'alt': 'Alt for img4'}     
}
$("input[type='radio']").click(function() {
 var imgObject = images[this.value];
 $('#'+this.name).attr('src', imgFldr+imgObject['img']).attr('alt', imgObject['alt']);
});

You could try something like this:

<img id="radio_btn1" src="originalimage1.jpg" />
<br />
<input type="radio" name="radio_btn1" value='image1' />
<input type="radio" name="radio_btn1" value='image2' />
<input type="radio" name="radio_btn1" value='image3' />
<input type="radio" name="radio_btn1" value='image4' />

imgFldr = 'images/nameofthesubfolder/';
var images = {
 image1: {'img': 'image1.jpg', 'alt': 'Alt for img1'},
 image2: {'img': 'image2.gif', 'alt': 'Alt for img2'},
 image3: {'img': 'image3.png', 'alt': 'Alt for img3'},
 image4: {'img': 'image4.jpeg', 'alt': 'Alt for img4'}     
}
$("input[type='radio']").click(function() {
 var imgObject = images[this.value];
 $('#'+this.name).attr('src', imgFldr+imgObject['img']).attr('alt', imgObject['alt']);
});
む无字情书 2024-07-25 06:50:08

只需复制粘贴到新页面...您有2 种方法来更改它

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head> 
<meta http-equiv="Content-type" content="text/html; charset=utf-8" /> 
<title>Sandbox</title> 

<style type="text/css" media="screen"> 
body { background-color: #000; font: 16px Helvetica, Arial; color: #fff; } 
</style> 

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> 

<script type="text/javascript"> 

imgFldr = 'http://www.kde-look.org/CONTENT/content-m1/'; 

$(document).ready(function(){

   $("input[type='radio']").each( function(index) {
    var $radio = this;
    var alt = '';

    switch (index + 1) {
        case 1: alt = 'alt 1'; break;
        case 2: alt = 'alt 2'; break;
        case 3: alt = 'alt 3'; break;
        case 4: alt = 'alt 4'; break;
    }
    console.log('img ' + (index+1) + ': ' + $radio.value + ' with alt: ' + alt);

    $(this).click(function() { 
    // 1st way: using mynewalt attribute of the radio button (you can delete the switch case if you choose to use this)
       $('#myImage').attr('src', imgFldr+$radio.value).attr('alt', $(this).attr('mynewalt'));
    //2nd way: using the switch
       $('#myImage').attr('src', imgFldr+$radio.value).attr('alt', alt);
    });
   }); 

}); 


</script> 


</head> 
<body> 
<img id="myImage" src="http://www.kde-look.org/CONTENT/content-m1/m100860-1.png" /> 
<br /> 
  <input type="radio" name="radio_btn1" value='m102389-1.png' mynewalt='new alt 1' /> 
  <input type="radio" name="radio_btn1" value='m100856-1.png' mynewalt='new alt 2' /> 
  <input type="radio" name="radio_btn1" value='m100857-1.png' mynewalt='new alt 3' /> 
  <input type="radio" name="radio_btn1" value='image4.jpeg' mynewalt='new alt 4' /> 

</body> 
</html>

just copy paste into a new page... you have 2 ways to change it.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head> 
<meta http-equiv="Content-type" content="text/html; charset=utf-8" /> 
<title>Sandbox</title> 

<style type="text/css" media="screen"> 
body { background-color: #000; font: 16px Helvetica, Arial; color: #fff; } 
</style> 

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> 

<script type="text/javascript"> 

imgFldr = 'http://www.kde-look.org/CONTENT/content-m1/'; 

$(document).ready(function(){

   $("input[type='radio']").each( function(index) {
    var $radio = this;
    var alt = '';

    switch (index + 1) {
        case 1: alt = 'alt 1'; break;
        case 2: alt = 'alt 2'; break;
        case 3: alt = 'alt 3'; break;
        case 4: alt = 'alt 4'; break;
    }
    console.log('img ' + (index+1) + ': ' + $radio.value + ' with alt: ' + alt);

    $(this).click(function() { 
    // 1st way: using mynewalt attribute of the radio button (you can delete the switch case if you choose to use this)
       $('#myImage').attr('src', imgFldr+$radio.value).attr('alt', $(this).attr('mynewalt'));
    //2nd way: using the switch
       $('#myImage').attr('src', imgFldr+$radio.value).attr('alt', alt);
    });
   }); 

}); 


</script> 


</head> 
<body> 
<img id="myImage" src="http://www.kde-look.org/CONTENT/content-m1/m100860-1.png" /> 
<br /> 
  <input type="radio" name="radio_btn1" value='m102389-1.png' mynewalt='new alt 1' /> 
  <input type="radio" name="radio_btn1" value='m100856-1.png' mynewalt='new alt 2' /> 
  <input type="radio" name="radio_btn1" value='m100857-1.png' mynewalt='new alt 3' /> 
  <input type="radio" name="radio_btn1" value='image4.jpeg' mynewalt='new alt 4' /> 

</body> 
</html>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文