【问题标题】:Dispaly only img src without displaying audio and video src using jquery使用jquery只显示img src不显示音视频src
【发布时间】:2021-08-10 10:18:53
【问题描述】:
    I want following conditions in my code basically i post filefield from backend and i want to display my filefield with respective sources:
    1. When i post image, My video and audio sources should be automatically hide.
    2.  When i post video, My image and audio sources should be automatically hide.
    3.  When i post audio, My video and image sources should be automatically hide.

My jquery code: In this code my else if part is not working.I am also using template literals in that code.
        

// 读取或检索博客数据(此函数调用创建、更新和删除成功消息)

    read_retrieve_blogs()
        
        function read_retrieve_blogs() {
            var card = document.getElementById('card');
            card.innerHTML = ''
            $.ajax({
                url: "http://localhost:8000/user_blogs/",
                cache: false,
                success: function(data) {
                    var list = data
                    for (var i in list) {
        
                        var item = ` 
        
                        
        
              <img src="${list[i].image}" class="card-img-top" id="r-image" height="280" width="280" alt="...">
        
        
              <video class="card-img-top" width='400' id="r-video" controls>
              <source src="${list[i].image}" type='video/mp4'>
              </video>
        
              <audio class="card-img-top" id="r-audio" controls width="320" height="240">
              <source src="${list[i].image}" type="audio/ogg">
              </audio>
              
            
              <div class="card-body" id="card-body">
              <h5 class="card-title">${list[i].title}</h5>
              <h5 class="card-title">Written by ${list[i].author}</h5>
              <h5 class="card-title">on ${list[i].date} | ${list[i].time}</h5>
              <p class="card-text">${list[i].description}</p>
              <div class="btn-toolbar" role="toolbar" aria-label="Toolbar with button groups">
              <div class="btn-group me-2" role="group" aria-label="First group">
              <button type="button" data-id="${list[i].id}" class="btn btn-success" 
              data-bs-toggle="modal" data-bs-target="#updateModal">Update</button>
              </div>
              <div class="btn-group me-2" role="group" aria-label="Second group">
              <input type="button" data-sid="${list[i].id}" class="btn btn-danger" value="Delete" 
              onclick="return confirm('Are You Sure For Delete?')">
              </div>
              </div>
              
                  `
        
                        card.innerHTML += item
        
                        if ($('#r-image').is('[src$=".png"],[src$=".jpeg"],[src$=".jpg"]')) {
        
                            $('#r-image:not([src=""])').show();
                            $('#r-video:not([src=""])').hide();
                            $('#r-audio:not([src=""])').hide();
        
        
        
        
                        } else if ($('#r-video').is('[src$=".mp4"]')) {
        
                            $('#r-image:not([src=""])').hide();
                            $('#r-video:not([src=""])').show();
                            $('#r-audio:not([src=""])').hide();
        
                        }
                    
                    }
                }
            });
        }
        
       
          I want following conditions in my code:
    1. When i post image, My video and audio sources should be automatically hide.
    2.  When i post video, My image and audio sources should be automatically hide.
    3.  When i post audio, My video and image sources should be automatically hide.

我希望在我的代码中包含以下条件: 1.当我发布图片时,我的视频和音频源应该会自动隐藏。 2.当我发布视频时,我的图像和音频源应该会自动隐藏。 3.当我发布音频时,我的视频和图像源应该会自动隐藏。

【问题讨论】:

  • 这能回答你的问题吗? How can I select an element by name with jQuery?
  • 一个 jQuery 对象总是真实的。如果您想知道它是否匹配任何内容,请测试长度。
  • 您还需要详细说明您要如何处理这些 src 属性
  • 现在,我更新了我的问题。请再次查看。
  • 并非如此。损坏的代码不能很好地替代书面解释。准确告诉我们您想要完成的代码尝试

标签: javascript html jquery ajax coding-style


【解决方案1】:

[src=".png"] 查找与 src 属性完全相同的图像。如果您只想测试 URL 的结尾,请使用$=

同一个#r-image 元素不可能有所有三个后缀,因此您应该使用|| 而不是&amp;&amp;。您还可以通过将选择器与 , 组合使用单个选择器来匹配其中任何一个。

要测试选择器是否匹配任何内容,请使用.length 属性。您还可以使用.is() 来测试一个元素是否与其他选择器匹配。

if $('#r-image').is('[src$=".png"],[src$=".jpeg"],[src$=".jpg"]'))

我的 HTML 代码是:

<img src="${list[i].image}" class="card-img-top" id="r-image" height="280" width="280" alt="...">

【讨论】:

  • 它给了我一个类似“#r-image”的错误。is is not a function
  • 现在我已经更新了我的问题,请再次查看。
  • @English Voice Quotes,使用:if ($('#r-image').is('[src$=".png"],[src$=".jpeg"],[src$=".jpg"]')){ // Some codes }。将 jquery 加入队列以防止 .is is not a function。请注意,您必须在"' 中设置元素,这意味着您必须使用$('#r-image').show(); 而不是$(#r-image).show();
  • 对不起,错字,我忘记了$
  • 我有一个新问题,我如何使用视频 src 文件格式编写相同的代码 嗨 Barmar,我从后端发布文件字段,并通过图像、视频和音频源中的 ajax 显示该文件字段。我想要在我的代码中完成三个任务: 1. 当我只发布图像时,我的视频和音频源应该自动隐藏 2. 当我只发布视频时,我的图像和音频源应该自动隐藏 3. 当我只发布音频源时, 我的视频和图片来源应该会自动隐藏
猜你喜欢
  • 1970-01-01
  • 2021-04-27
  • 2020-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-26
相关资源
最近更新 更多