英文:
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() {
$('#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() {
// Start observing the target node for configured mutations
observer.observe(targetNode, config);
// Later, you can stop observing
//observer.disconnect();
});
<!-- language: lang-html -->
<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>
<!-- 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 = $('.thumbnail').length;
if ( count > 0 ) {
$('#submit').hide();
} else {
$('#submit').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.
$("img").bind('load', toggleUploadButton );
$(".closebutton").click(function(){
// remove the thumbnail then..
toggleUploadButton();
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论