如果存在特定的id,则隐藏元素,即使它在页面加载后出现

huangapple go评论68阅读模式
英文:

Hide element if specific id exists, even if it appears after page load

问题

我有一个简单的图像上传器,中间有一个“浏览文件”按钮。我尝试在选择图像后隐藏此上传按钮,以便我可以显示图像缩略图,而不让上传按钮挤占一切。

这是我所拥有的内容,但它不起作用,我不确定我漏掉了什么(或者是否有更好的方法来完成这个任务)。如果有更好的方法,我愿意尝试,我只是尝试了这个,因为看起来应该是一个简单的事情,如果我没有犯错,它应该可以满足我的需求。有什么建议吗?

$(document).ready(function(){
    if($('#thumbnail').length){
        $('#submit').hide();
    }
});

我需要它始终“监听”图像何时出现,因为它可能在任何时候发生。如果有关系的话,这个片段位于一个.js文件中...我不确定这是否是我的错误,或者这是否对此没有影响。理想情况下,每个图像都会有一个X来删除缩略图,如果所有图像都被删除,那么上传按钮将再次出现。我不知道我正在尝试的是否会达到这个效果,或者是否需要添加第二部分来再次显示它。

感谢任何帮助。谢谢。

英文:

I have a simple image uploader with a 'browse files' button in the middle. I'm trying to hide this upload button once images have been selected so I can display the image thumbnails without the upload button crowding everything.

Here is what I have but it's not working and i'm not sure what i'm missing (or if there's a better way to do this altogether. I am open to a whole other method of doing this if there's a better way, I just tried this because it seemed like a simple thing that should have done what I needed if I didn't make a mistake. Any suggestions?

$(document).ready(function(){
if($('#thumbnail').length){
$('#submit').hide();}
});

I need this to always be "listening" for the images to show up because it could happen at any time. If it maters, this snippet is inside of a .js file...I wasn;t sure if that was my mistake or if that should have no effect on this. Ideally I'll have an X on each image to remove the thumbnails and if all the images are removed then the upload button will appear again. I have no idea if what i'm attempting will do that as it is, or if I have to add a second bit to show it again.

Any help is appreciated. Thank you.

答案1

得分: 2

编辑:
正如@Ruan Mendes在评论中指出的那样,要在加载图像的同一段代码中隐藏按钮会更容易。但如果您无法控制该代码,MutationObserver 是唯一的选择。

还要查看这个答案,了解关于性能的提示,如果您的页面非常大/复杂。


您可以使用MutationObserver来监视HTML中的更改。

function getImage() {
  $('#target').append('<div id="thumbnail">Thumbnail</div>');
}
const targetNode = document.getElementById("target");

const config = {
  attributes: false,
  childList: true,
  subtree: true
};

const callback = (mutationList, observer) => {
  for (const mutation of mutationList) {
    if (mutation.type === "childList") {
      mutation.addedNodes.forEach(node => {
        if (node.nodeType == 1 && node.id == "thumbnail") {
          $('#submit').hide();
        }
      });
    }
  }
};
const observer = new MutationObserver(callback);

$(document).ready(function() {

  // 开始观察目标节点以配置的变化
  observer.observe(targetNode, config);

  // 以后,您可以停止观察
  //observer.disconnect();

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button onclick="getImage()">Click me</button>
<div id="target"></div>
<button id="submit">Submit</button>
英文:

Edit:
As @Ruan Mendes pointed out in comment, it'd be much easier to hide the button in the same piece of code that load the image. But if you don't have control over that code, MutationObserver is the only choice.

Also check this answer for tips about performance if your page is very large/complex.


You can use MutationObserver to monitor changes in HTML.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function getImage() {
  $(&#39;#target&#39;).append(&#39;&lt;div id=&quot;thumbnail&quot;&gt;Thumbnail&lt;/div&gt;&#39;);
}
const targetNode = document.getElementById(&quot;target&quot;);

const config = {
  attributes: false,
  childList: true,
  subtree: true
};

const callback = (mutationList, observer) =&gt; {
  for (const mutation of mutationList) {
    if (mutation.type === &quot;childList&quot;) {
      mutation.addedNodes.forEach(node =&gt; {
        if (node.nodeType == 1 &amp;&amp; node.id == &quot;thumbnail&quot;) {
          $(&#39;#submit&#39;).hide();
        }
      });
    }
  }
};
const observer = new MutationObserver(callback);



$(document).ready(function() {

  // Start observing the target node for configured mutations
  observer.observe(targetNode, config);

  // Later, you can stop observing
  //observer.disconnect();

});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;button onclick=&quot;getImage()&quot;&gt;Click me&lt;/button&gt;
&lt;div id=&quot;target&quot;&gt;&lt;/div&gt;
&lt;button id=&quot;submit&quot;&gt;Submit&lt;/button&gt;

<!-- end snippet -->

答案2

得分: 0

开始编写一个函数根据可见的缩略图图像数量来显示或隐藏按钮...

function toggleUploadButton(){
   const count = $('.thumbnail').length;
   if ( count > 0 ) {
       $('#submit').hide();
   } else {
       $('#submit').show();
   }
}

然后你需要两个事件处理函数一个在缩略图图像加载时触发另一个在缩略图图像被移除时触发两者都会调用上面的toggleUploadButton函数

$("img").bind('load', toggleUploadButton );


$(".closebutton").click(function(){
     // 移除缩略图然后..
     toggleUploadButton();
});
英文:

Start with a function that shows or hides the button depending on how many thumbnail images are visible...

function toggleUploadButton(){
   const count = $(&#39;.thumbnail&#39;).length;
   if ( count &gt; 0 ) {
       $(&#39;#submit&#39;).hide();
   } else {
       $(&#39;#submit&#39;).show();
   }
}

Then you will need two event handler functions. One that fires when a thumbnail image loads, and another when it is removed. Both will call the toggleUploadButton function above.

$(&quot;img&quot;).bind(&#39;load&#39;, toggleUploadButton );


$(&quot;.closebutton&quot;).click(function(){
     // remove the thumbnail then..
     toggleUploadButton();
});

huangapple
  • 本文由 发表于 2023年6月2日 06:51:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76386172.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定