how to get img src value

2019-02-09 14:40发布

问题:

I have some contents inside div tag...

within that div tag content I have to search for img src tag value

based on that value i have to highlight some images and to show some div content

for example

if img src value contains "http://google.com/test/test.img" have to highlight and to show img is highlighted div content

if img src value contains some specific path "news/images/test1.jpg" have to highlight and to show img is highlighted div content

if img src value contains some specific path "news/articles/images/test1.gif" no need to highlight and to show img is not highlighted div content.

回答1:

I think you mean that there are two possible scenarios where you want to highlight the image:

var $img = $("#someImage");
var src = $img.attr("src");
if(src == 'http://google.com/test/test.img' || src == 'news/images/test1.jpg') {
    $img.addClass("highlight");
    // or
    $img.css("border", "3px solid yellow");
}

EDIT based on your comment:

$("#formpreview img[src*=google.com]").addClass("highlight");


回答2:

$('#someImage[url="http://google.com/test/test.img"],'
+ '#someImage[url="news/image/test1.jpg"]')
.addClass("highlight").css("border", "3px solid yellow");