简体   繁体   English

初学者/我尝试使用 Onclick 取消隐藏元素,但它不起作用

[英]Beginner/ I try to use Onclick to unhide element but it doesn't work

I want to make a page to upload the avatar.我想制作一个页面来上传头像。 By default, I use the vector to show where the image will appear and then provide a button to upload the URL to change the avatar.默认情况下,我使用矢量来显示图像将出现的位置,然后提供一个按钮来上传 URL 以更改头像。 That's all it is.就是这样。 But the script still doesn't work.但是脚本仍然不起作用。 Pleased to hear your feedback on how to fix it.很高兴听到您对如何修复它的反馈。 Bless保佑

<img id="put_image_here_bitch" src="https://cdn0.iconfinder.com/data/icons/finance-vol-2-4/48/77-512.png" alt="" width="100px" height="100px">
<div class="block" > The person who uploads this is cool
</div>

<button onclick="hideElement()">Click to upload photo by URL</button>

<div>
    <input id="input" autofocus class='hidden_element' style="display: none;" type="text" id="input">
</div>
<div>
    <button class='hidden_element' style="display: none;" onclick="uploadImage()">UPLOAD</button>

    
</div>

This is my script这是我的脚本

 function hideElement(){
    var hide = document.getElementsByClassName('hidden_element');
    if (hide.style.display === "none") {
    hide.style.display = "block";
    } else {
    hide.style.display = "none";
    }
}

var uploadImage = function(){
    image = document.getElementById('input').value;
    showImage = document.getElementById('put_image_here_bitch').setAttribute('src', image);

    
};

As stated in the comments, .getElementsByClassName() returns a collection of elements, not a single element and your code attempts to call the style property of the collection, which doesn't exist.如评论中所述, .getElementsByClassName()返回元素的集合,而不是单个元素,并且您的代码尝试调用不存在的集合的style属性。

Instead, you need to loop through the collection and operate on the elements within the collection individually, but don't use .getElementsByClassName() and instead use .querySelectorAll() .相反,您需要遍历集合并单独操作集合中的元素,但不要使用.getElementsByClassName()而是使用.querySelectorAll()

 var hidden = document.querySelectorAll('.hidden_element'); function hideElement(){ // Loop over the colleciton elements hidden.forEach(function(element){ if (element.style.display === "none") { element.style.display = "block"; } else { element.style.display = "none"; } }); } var uploadImage = function(){ image = document.getElementById('input').value; showImage = document.getElementById('put_image_here_bitch').setAttribute('src', image); };
 <img id="put_image_here_bitch" src="https://cdn0.iconfinder.com/data/icons/finance-vol-2-4/48/77-512.png" alt="" width="100px" height="100px"> <div class="block" > The person who uploads this is cool </div> <button onclick="hideElement()">Click to upload photo by URL</button> <div> <input id="input" autofocus class='hidden_element' style="display: none;" type="text" id="input"> </div> <div> <button class='hidden_element' style="display: none;" onclick="uploadImage()">UPLOAD</button> </div>

But, beyond that, you should also avoid using inline styles as they are the most specific way of setting a style and therefore the hardest to override.但是,除此之外,您还应该避免使用内联 styles,因为它们是设置样式的最具体方式,因此最难覆盖。 They also often require duplicated code to be written.他们还经常需要编写重复的代码。 Instead, use CSS classes as shown below:相反,请使用 CSS 类,如下所示:

 // Get references to the DOM elements that you'll need to work with const btnUpload = document.querySelector("button"); // find the first button const hidden = document.querySelectorAll(".hidden"); const upload = document.querySelector(".upload"); // Do your event binding in JavaScript, not in HTML btnUpload.addEventListener("click", hideElement); upload.addEventListener("click", uploadImage); function hideElement(){ // Loop over the collection of hidden elements hidden.forEach(function(item){ // See how much more simple it is to work with classes? item.classList.toggle("hidden"); }); } function uploadImage(){ showImage = document.getElementById('put_image_here_bitch').setAttribute('src', input.value); };
 .hidden { display:none; }
 <img id="put_image_here_bitch" src="https://cdn0.iconfinder.com/data/icons/finance-vol-2-4/48/77-512.png" alt="" width="100px" height="100px"> <div class="block" > The person who uploads this is cool </div> <button>Click to upload photo by URL</button> <div> <input id="input" autofocus class='hidden' type="text" id="input"> </div> <div> <button class='hidden upload'>UPLOAD</button> </div>

simple.. getElementsByClassName() returns an HTMLCollection with all DOM elements containing that class.简单.. getElementsByClassName()返回一个 HTMLCollection,其中包含包含该 class 的所有 DOM 元素。 An HTMLCollection is like an array ( but not really ) containing element references. HTMLCollection 就像一个包含元素引用的数组(但不是真的)。

thus you need to define which entry in the array you want to handle ( even if there's only one )因此您需要定义要处理的数组中的哪个条目(即使只有一个)

your code should work by simply adding [0] to your DOM read ( the '0' means the first element in the collection )您的代码应该通过简单地将[0]添加到您的 DOM 读取来工作(“0”表示集合中的第一个元素)

ex:前任:

var hide = document.getElementsByClassName('hidden_element')[0];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM