Javascript 将类添加到特定映像的父div

Javascript 将类添加到特定映像的父div,javascript,jquery,underscore.js,instagram,Javascript,Jquery,Underscore.js,Instagram,然后它转到HTML文件并围绕该文件构建库 (function(){ var photoTemplate, resource; function init(){ bindEventHandlers(); photoTemplate = _.template($('#photo-template').html()); } function toTemplate(photo){ photo = { count: photo.likes.cou

然后它转到HTML文件并围绕该文件构建库

  (function(){
  var photoTemplate, resource;

  function init(){
    bindEventHandlers();
    photoTemplate = _.template($('#photo-template').html());
  }

  function toTemplate(photo){
    photo = {
      count: photo.likes.count,
      avatar: photo.user.profile_picture,
      photo: photo.images.low_resolution.url,
      url: photo.link
    };

    return photoTemplate(photo);
  }

  function toScreen(photos){
    var photos_html = '';

    $('.paginate a').attr('data-max-tag-id', photos.pagination.next_max_id)
                    .fadeIn();

    $.each(photos.data, function(index, photo){
      photos_html += toTemplate(photo);
    });

    $('div#photos-wrap').append(photos_html);
  }

  function generateResource(tag){
    var config = Instagram.Config, url;

    if(typeof tag === 'undefined'){
      throw new Error("Resource requires a tag. Try searching for cats.");
    } else {
      // Make sure tag is a string, trim any trailing/leading whitespace and take only the first 
      // word, if there are multiple.
      tag = String(tag).trim().split(" ")[0];
    }

    url = config.apiHost + "/v1/tags/" + tag + "/media/recent?callback=?&client_id=" + config.clientID;

    return function(max_id){
      var next_page;
      if(typeof max_id === 'string' && max_id.trim() !== '') {
        next_page = url + "&max_id=" + max_id;
      }
      return next_page || url;
    };
  }


function paginate(max_id){    
        $.getJSON(generateUrl(tag), toScreen);
      }

      function search(tag){
        resource = generateResource(tag);
        $('.paginate a').hide();
        $('#photos-wrap *').remove();
        fetchPhotos();
      }

      function fetchPhotos(max_id){
        $.getJSON(resource(max_id), toScreen);
      }

      function bindEventHandlers(){
        $('body').on('click', '.paginate a.btn', function(){
          var tagID = $(this).attr('data-max-tag-id');
          fetchPhotos(tagID);
          return false;
        });

        // Bind an event handler to the `submit` event on the form
        $('form').on('submit', function(e){

          // Stop the form from fulfilling its destinty.
          e.preventDefault();

          // Extract the value of the search input text field.
          var tag = $('input.search-tag').val().trim();

          // Invoke `search`, passing `tag` (unless tag is an empty string).
          if(tag) {
            search(tag);
          };

        });

      }

      function showPhoto(p){
        $(p).fadeIn();
      }

      // Public API
      Instagram.App = {
        search: search,
        showPhoto: showPhoto,
        init: init
      };
    }());

    $(function(){
      Instagram.App.init();

      // Start with a search on yogofactory; we all love frozen yogurt :).
      Instagram.App.search('yogofactory');  
    });
对于JS或jQuery,我没有什么经验。我正在寻找一种方法,当其中的图像是src时,向父级添加一个类=http://image.com/onlythisone.jpg 而且只有当它的形象

我有一个图库一个从instagram数据库动态提取的大型图库,我基本上希望能够隐藏某些不符合我为促销创建的哈希标记规则的照片

任何帮助都会很棒!提前谢谢

<script type="text/template" id="photo-template">
      <div class='photo'>
          <img class='main' src='<%= photo %>' width='250' height='250' style='display:none;' onload='Instagram.App.showPhoto(this);' />
        <img class='avatar' width='40' height='40' src='<%= avatar %>' />
        <span class='heart'><strong><%= count %></strong></span>
      </div>
</script>
编辑:

编辑:


下面是一个用javascript编写的函数,当使用设置为图像的src值和类名的属性调用该函数时,将搜索所有图像并将指定的类添加到其父类(如果该类不存在)。只需在加载任何图像后运行该函数

function toScreen(photos) {
    var photos_html = '';

    $('.paginate a').attr('data-max-tag-id', photos.pagination.next_max_id)
        .fadeIn();

    $.each(photos.data, function (index, photo) {
        photos_html += toTemplate(photo);
    });

    photos_html.find('img[src="http://image.com/onlythisone.jpg"]').remove();

    $('div#photos-wrap').append(photos_html);
}
)

更新:根据您现在发布的代码,您将添加对上述函数的调用,如下所示

function addClassToSpecificImageParent(imageUrl, className) {
    Array.prototype.forEach.call(document.getElementsByTagName("img"), function (element) {
        if (element.src === imageUrl && !element.parentNode.classList.contains(className)) {
            element.parentNode.classList.add(className);
        }
    });
}

addClassToSpecificImageParent("http://image.com/onlythisone.jpg", "someClass");
更新:现在您已经发布了更多详细信息,另一种方法是使用下划线模板

HTML-下划线模板

function toScreen(photos) {
    var photos_html = '';

    $('.paginate a').attr('data-max-tag-id', photos.pagination.next_max_id)
        .fadeIn();

    $.each(photos.data, function (index, photo) {
        photos_html += toTemplate(photo);
    });

    $('div#photos-wrap').append(photos_html);

    addClassToSpecificImageParent("http://image.com/onlythisone.jpg", "someClass");
}

下面是一个用javascript编写的函数,当使用设置为图像的src值和类名的属性调用该函数时,将搜索所有图像并将指定的类添加到其父类(如果该类不存在)。只需在加载任何图像后运行该函数

function toScreen(photos) {
    var photos_html = '';

    $('.paginate a').attr('data-max-tag-id', photos.pagination.next_max_id)
        .fadeIn();

    $.each(photos.data, function (index, photo) {
        photos_html += toTemplate(photo);
    });

    photos_html.find('img[src="http://image.com/onlythisone.jpg"]').remove();

    $('div#photos-wrap').append(photos_html);
}
)

更新:根据您现在发布的代码,您将添加对上述函数的调用,如下所示

function addClassToSpecificImageParent(imageUrl, className) {
    Array.prototype.forEach.call(document.getElementsByTagName("img"), function (element) {
        if (element.src === imageUrl && !element.parentNode.classList.contains(className)) {
            element.parentNode.classList.add(className);
        }
    });
}

addClassToSpecificImageParent("http://image.com/onlythisone.jpg", "someClass");
更新:现在您已经发布了更多详细信息,另一种方法是使用下划线模板

HTML-下划线模板

function toScreen(photos) {
    var photos_html = '';

    $('.paginate a').attr('data-max-tag-id', photos.pagination.next_max_id)
        .fadeIn();

    $.each(photos.data, function (index, photo) {
        photos_html += toTemplate(photo);
    });

    $('div#photos-wrap').append(photos_html);

    addClassToSpecificImageParent("http://image.com/onlythisone.jpg", "someClass");
}


获得经验的唯一方法就是自己尝试。那么$'img[src]呢=http://image.com/onlythisone.jpg]'.parent.addClass'your_class'?toScreen函数在哪里?获得经验的唯一方法是自己尝试。那么$'img[src]呢=http://image.com/onlythisone.jpg]'.parent.addClass'your_class'?toScreen函数在哪里?这看起来并不能解决任何与OP描述的内容相近的问题。@斜视-当源与确切的字符串匹配时,它会向图像的父级添加一个类,听起来与OP要求的完全一样?如果它是静态URL,并且只需要在页面加载时出现,那当然。但是这个问题描述了比这更复杂的情况。@眯着眼-那么我们不是在读同一个问题吗?它表示OP正在寻找一种方法,向任何图像的父元素添加一个类,该图像的src属性与某个字符串匹配?@RaufTür-我仍然缺少toScreen函数,这是最重要的一点?这看起来并不能解决任何与OP描述的内容相近的问题。@斜视-当源与确切的字符串匹配时,它会向图像的父级添加一个类,这听起来正是OP所要求的?如果它是一个静态URL,并且只需要在页面加载时出现,那么请确定。但是这个问题描述了比这更复杂的情况。@眯着眼-那么我们不是在读同一个问题吗?它表示OP正在寻找一种方法,向任何图像的父元素添加一个类,该图像的src属性与某个字符串匹配?@RaufTür-我仍然缺少toScreen函数,这是一个重要的函数?谢谢!在构建库之后,我将如何运行此函数?只需在构建库之后的代码中调用函数,如上所示。查看您的更新,可能是作为toScreen函数的最后一个操作的一部分。否则,唯一的其他方法是使用突变事件/观察者来观察DOM的更改,并在看到添加或修改的图像时执行该函数。将其添加到toScreen部分的最后一部分似乎是可行的!非常感谢你的时间和帮助!没问题,当然你可以用@adeneo jquery解决方案做同样的事情。谢谢!在构建库之后,我将如何运行此函数?只需在构建库之后的代码中调用函数,如上所示。查看您的更新,可能是作为toScreen函数的最后一个操作的一部分。否则,唯一的其他方法是使用突变事件/观察者来观察DOM的更改,并在看到添加或修改的图像时执行该函数。将其添加到toScreen部分的最后一部分似乎是可行的!非常感谢你的时间和帮助!没问题,当然你可以用@adeneo jquery解决方案做同样的事情。