可以使用图像URL为图像动态添加替代文本
dynamically add alternate text for your Image using the image URL is it possible?
我有一个问题,需要根据图像源更改替换文本。例如,我有一个标记为图像的文件夹,其中包含用于该项目的所有图像。
<table id="OptionPics">
<tr><td><img id="OptionPic1" alt="" src="images/selection1.png" /></td></tr>
<tr><td><img id="OptionPic2" alt="" src="images/selection2.png" /></td></tr>
<tr><td><img id="OptionPic3" alt="" src="images/selection3.png" /></td></tr>
</table>
<table id="SelectedPicTable">
<tr>
<td>
<img id="SelectedPic" alt="" src="images/placeholder.png" />
</td>
</tr>
</table>
我想知道的是,是否可以根据单击的图像更改SelectedPic的alt?例如,如果单击OptionPic1,SelectedPic的alt应为"selection1"而不是images/selection1.png"。
如果你没有心情使用jQuery,这里是纯JS:
http://jsfiddle.net/MaxPRafferty/6YUhg/
HTML:
<table id="OptionPics">
<tr><td><img class="OptionPic1 optPics" alt="" src="http://jsfiddle.net/img/logo.png?image=1" /></td></tr>
<tr><td><img class="OptionPic2 optPics" alt="" src="http://jsfiddle.net/img/logo.png?image=2" /></td></tr>
<tr><td><img class="OptionPic3 optPics" alt="" src="http://jsfiddle.net/img/logo.png?image=3" /></td></tr>
</table>
<table id="SelectedPicTable">
<tr><td><img id="SelectedPic" alt="" src="http://jsfiddle.net/img/logo.png" /></td></tr>
</table>
JS:
var imgs = document.getElementsByClassName("optPics");
for(var i = 0; i < imgs.length; i++)
{
(function(image){
image.onclick=function(){
var imageParts = image.src.split('/');
var imageName = imageParts[imageParts.length-1];
document.getElementById("SelectedPic").alt=imageName;
document.getElementById("SelectedPic").title=imageName;
}
}(imgs[i]))
}
关于以上代码的几个快速注释:请注意,我还更改了title属性-在某些版本的FireFox中,更改alt没有效果,而且其他版本可能也会忽略对alt属性的更改。更改"title"属性会更改悬停文本。我把两者都包括在内以供参考。
此外,我会根据您的要求剥离图像路径,但如果由于某种原因,您的图像在文件名后面会有"/"字符,则需要修改此代码。在我的例子中,我用查询字符串来区分图像,所以我不会去掉扩展名,但如果您只想有文件名,请将相应的行更改为var imageName = imageParts[imageParts.length-1].split('.')[0];
如何控制图像中的点击?它是纯javascript,还是使用jQuery?
今天,使用jQuery非常常见,我假设您正在使用它:
<table id="OptionPics">
<tr><td><img id="OptionPic1" class="options" alt="" src="images/selection1.png" /></td></tr>
<tr><td><img id="OptionPic2" class="options" alt="" src="images/selection2.png" /></td></tr>
<tr><td><img id="OptionPic3" class="options" alt="" src="images/selection3.png" /></td></tr>
</table>
<table id="SelectedPic">
<tr><td><img id="SelectedPic" alt="" src="images/placeholder.png" /></td></tr>
</table>
看,我在所有图像中插入了一个名为options的类。现在让我们编写jQuery:
$(document).ready(function(){
$('img.options').on('click', function(){ //everytime a image is clicked
var i = $(this); //handle to the clicked image
$('img#SelectedPic').attr('src', i.attr('src')).attr('alt', i.attr('alt'));
//insert into image with the SelectedPic ID, the same src and alt attributes of the clicked image
});
});
相关文章:
- 将gif图像添加到chart.js V2.0中
- 如何通过选择器元素将图像添加到javascript下拉警报消息中
- 使用chosen.js,如何将图像添加到下拉项中
- jQuery动态地将图像添加到表中
- 使用javascript为网页中的所有图像添加属性
- 使用javascript根据图像的布局方向为图像添加不同的类名
- 如何将淡出图像添加到我的Django页面
- AngularJS:为ng-repeat中的图像添加切换功能
- 将图像添加到 json 搜索栏
- 使用jquery为基于alt文本的一些图像添加一个类
- 正在向库图像添加链接
- 是否可以在生成器中为图像添加权重
- 如何将图像添加到每个画布
- 如何将叠加图像添加到SIMILE Exhibit地图视图中
- FabricJS将图像添加到组会导致奇怪的控制行为
- 当所有表单字段都完成时,jquery将图像添加到列表项中一次
- 如何在CKEditor中为图像添加ID
- 有没有任何方法可以使用joomla为图像添加描述!模态挤压盒
- 将图像添加到svg
- 将第二个背景图像添加到元素中