我在jQuery中有一个遍历每个“div.panel”的foreach循环,我需要访问这个div中图像的img src……
HTML:
<div class="panel" title="Sean Gay">
<div class="wrapper"><img src="/Media/people/SeanGay.jpg" alt="Sean Gay" class="person">
<div class="vcard"><span class="fn">Sean Gay</span> <span class="title">Chief Storeman</span>
</div>
</div>
</div>jQuery:
jQuery(this).find("div.panel").each(function(n) {
var title = jQuery(this).attr("title");这将获得面板的标题,在此循环中,我需要包装器中不带.jpg扩展名的图像的src。
我该怎么做呢?
发布于 2012-03-20 22:10:40
这只是基本的遍历和字符串操作:
jQuery(this).find("div.panel").each(function(n) {
// Get the panel as a jQuery instance
var panel = jQuery(this);
// Get its title
var title = panel.attr("title");
// Find the first image and get its `src` property; note that here
// I'm assuming there *will* be one. If that assumption isn't true,
// cache the img lookup and use `if (img[0])` to guard.
var src = panel.find("img")[0].src;
// Remove ".jpg" at the end if it's there
src = src.replace(/\.jpg$/i, "");
});发布于 2012-03-20 22:16:15
$(function(){
$("div.panel").each(function(n){
var imageSrc=$(this).find("img").attr("src");
var ary = imageSrc.split("/");
var onlyFileName=ary[ary.length - 1];
var imageSrcWithioutExtension=onlyFileName.replace(".JPG", "").replace(".jpg", "");;
alert(imageSrcWithioutExtension)
});
});以下是工作示例:http://jsfiddle.net/QSgtg/24/
https://stackoverflow.com/questions/9788417
复制相似问题