简体   繁体   English

如何在初始页面加载时使用CSS隐藏和元素,直到单击Javascript链接

[英]How can I hide and element using CSS on initial page load until the Javascript link is clicked

I currently have a webpage that looks like this: 我目前有一个网页,看起来像这样:

在此处输入图片说明

I would like to make it so the "Click to view Gallery of This property" button does not show on the initial load, but does show once the property links are clicked. 我想这样做,因此“单击以查看此属性的库”按钮在初始加载时不会显示,但是在单击属性链接后会显示。

Here is an example of my current code: 这是我当前代码的示例:

    function changeImage(image, link) {
    document.getElementById('imageReplace').src = image;
    document.getElementById('linkReplace').href = link;
}

HTML: HTML:

<a href="#/" onclick="changeImage('IMAGE', 'LINK');  return false">TEXT</a>
<a href="#/" onclick="changeImage('IMAGE', 'LINK');  return false">TEXT</a>
<a href="#/" onclick="changeImage('IMAGE', 'LINK');  return false">TEXT</a>

<img src="INITALPAGELOADIMAGE" alt="Images" id="imageReplace" class="changeImageClass">

<a href="#" id="linkReplace"><span style="float: right; padding:2px;"><Button>Click to view Gallery of This Property</button></span></a>

From what I understand I will want to use to use this function to hide the element: 据我了解,我将要使用此功能来隐藏元素:

document.getElementById('element').style.display = 'none';

But I am unsure how to work this into my current code. 但是我不确定如何在当前代码中使用它。 Do I add this with my current function, or is this a separate function all together? 我要在当前函数中添加它,还是这是一个单独的函数?

If someone could point me into the right direction of how to code this properly I would really appreciate it. 如果有人可以指出正确编码方法的正确方向,我将不胜感激。

Thank you so much, -Kasandra 非常感谢你-Kasandra

I think this is one of those exceptional cases where you want to use inline style to hide your element. 我认为这是您要使用内联样式来隐藏元素的特殊情况之一。 This is because it takes a few seconds for the JavaScript to load. 这是因为加载JavaScript需要花费几秒钟的时间。 So some users may end up seeing the element when they first get on the page, then see it quickly disappear. 因此,某些用户初次进入页面时可能会看到该元素,然后很快消失。

<img src="INITALPAGELOADIMAGE" alt="Images" id="imageReplace" class="changeImageClass" style="display: none;">
<a href="#" id="linkReplace" style="display: none"><span style="float: right; padding:2px;"><Button>Click to view Gallery of This Property</button></span></a>

Add an onclick attribute to your property links, 在属性链接中添加onclick属性,

<a href="#" onclick="changeImage('IMAGE', 'LINK'); return false;">TEXT</a>

And use the following function to show the elements 并使用以下函数显示元素

function changeImage (src, href) {
    // get a reference to the DOM elements
    var img = document.getElementById('imageReplace'),
        link = document.getElementById('linkReplace');
    // update the elements
    img.src = src;
    link.href = href;
    // show the elements
    img.style.display = '';
    link.style.display = '';
}

Here's a quick demo (updated) . 这是一个快速演示(已更新)

Using css, you can have the block have display: none; 使用css,您可以使块display: none; and then when you use show it will change the display to default (usually display: block; ) 然后当您使用show时,它将显示更改为默认display: block; (通常是display: block;

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

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