在 Javascript 中使用 href=mailto 链接图像

发布于 2024-12-15 14:03:03 字数 2375 浏览 2 评论 0原文

我做了一个主页。如果您单击任意位置,则背景需要更改,除非您单击带有联系人的图片。如果您单击带有联系人的图片,则会发送一封邮件以获取电子邮件地址。

现在一切正常,唯一的问题是当我单击带有 a href mailto 的图片时,背景消失了。我不知道为什么它的工作方式不同,然后当我点击其他地方时.. 这是主页: http://staehelinmeyer.carolburri.com/

一些代码:

<script type="text/javascript">
    var x=1;        //store which picture to show
    var MAX=10;     //store how much picture is
    var n=1;        //count until 10
    var y=x;        //prevent to not put the same image after itself
    function imgchanger(){  //changes the image
        n++;        //count until 10
        x= Math.floor(Math.random()*(MAX-1))+2;     //generate a random number between 2 and MAX
        if(x==y){   //if its the same image like what was before
            while(x==y){x= Math.floor(Math.random()*(MAX-1))+2;}    //generate a new number
        }
        if(n==MAX){     //if its the MAX time of clicking
            x=1;        //show the first picture
            n=1;        //and begin the counting from one
        }
        //change the picture
        document.getElementById("html").style.backgroundImage = "url(files/"+x+".jpg)";
        if (x==1){  //if its the first picture show the footer and the contact
            document.getElementById("contact_name").style.visibility='visible';
            document.getElementById("footer").style.visibility='visible';
        }
        else{       //else hide the footer and the contact
            document.getElementById("contact_name").style.visibility='hidden';
            document.getElementById("footer").style.visibility='hidden';
        }
        y=x;        //save what was the picture
     }    
</script>

<body onclick="imgchanger()">
<div id="page-wrap">
    <div style="height:0px; position:fixed; top:30px; right:5px; background-color:#f0f0f0;">
        <img alt="contact_name" id="contact_name" src="files/contact_name.png" />
        <a href="mailto:[email protected]">
            <img alt="contact" src="files/contact.png"/>
        </a>
    </div>
    <div id="footer" class="footer">
        Sample text
    </div>
</div>
</body>

I made a homepage. If you click anywhere the background needed to be changed, except if you click in to the picture with contact. If you click in that picture with contact, a mail should send for an email-address.

Now everything is working, the only problem is when I click to the a picture with a href mailto the background disappear. I have no idea why it works differently, then when I click somewhere else..
Here is the hompage: http://staehelinmeyer.carolburri.com/

Some code:

<script type="text/javascript">
    var x=1;        //store which picture to show
    var MAX=10;     //store how much picture is
    var n=1;        //count until 10
    var y=x;        //prevent to not put the same image after itself
    function imgchanger(){  //changes the image
        n++;        //count until 10
        x= Math.floor(Math.random()*(MAX-1))+2;     //generate a random number between 2 and MAX
        if(x==y){   //if its the same image like what was before
            while(x==y){x= Math.floor(Math.random()*(MAX-1))+2;}    //generate a new number
        }
        if(n==MAX){     //if its the MAX time of clicking
            x=1;        //show the first picture
            n=1;        //and begin the counting from one
        }
        //change the picture
        document.getElementById("html").style.backgroundImage = "url(files/"+x+".jpg)";
        if (x==1){  //if its the first picture show the footer and the contact
            document.getElementById("contact_name").style.visibility='visible';
            document.getElementById("footer").style.visibility='visible';
        }
        else{       //else hide the footer and the contact
            document.getElementById("contact_name").style.visibility='hidden';
            document.getElementById("footer").style.visibility='hidden';
        }
        y=x;        //save what was the picture
     }    
</script>

<body onclick="imgchanger()">
<div id="page-wrap">
    <div style="height:0px; position:fixed; top:30px; right:5px; background-color:#f0f0f0;">
        <img alt="contact_name" id="contact_name" src="files/contact_name.png" />
        <a href="mailto:[email protected]">
            <img alt="contact" src="files/contact.png"/>
        </a>
    </div>
    <div id="footer" class="footer">
        Sample text
    </div>
</div>
</body>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

糖果控 2024-12-22 14:03:03

添加此代码:

$("a").bind("click", function(e) {
    e.stopPropagation();
});

Add this code:

$("a").bind("click", function(e) {
    e.stopPropagation();
});
暗地喜欢 2024-12-22 14:03:03

有点像穆尼姆·阿卜杜勒所说的,尝试添加以阻止传播

       <a href="mailto:[email protected];" onclick="event.stopPropagation();">

a bit like what Munim Abdul said try you could add to stop propagation

       <a href="mailto:[email protected];" onclick="event.stopPropagation();">
愛放△進行李 2024-12-22 14:03:03

尝试这个功能:

function imgchanger(){  
    var x = Math.floor(Math.random()*11) //sets x to a random number between 1 and 10

    document.getElementById("html").style.backgroundImage = "url(files/" + x + ".jpg)";      
    document.getElementById("contact_name").style.visibility='hidden';
    document.getElementById("footer").style.visibility='hidden';
}

默认情况下,“contact_name”和“footer”元素应该是可见的。该代码生成一个 1 到 10 之间的随机数,然后隐藏您想要隐藏的两个元素,并且比您的代码小得多。

如果问题仍然存在,请再次评论,我会再看一下。

Try this function:

function imgchanger(){  
    var x = Math.floor(Math.random()*11) //sets x to a random number between 1 and 10

    document.getElementById("html").style.backgroundImage = "url(files/" + x + ".jpg)";      
    document.getElementById("contact_name").style.visibility='hidden';
    document.getElementById("footer").style.visibility='hidden';
}

The "contact_name" and "footer" elements should be visible to start off with by default. This code produces a random number between 1 and 10 and then hides the two elements you want hidden and is much smaller than your code.

If the problem persists comment again here and I will have another look at it.

多像笑话 2024-12-22 14:03:03

我测试了一下,看起来不错,但是当我点击mailto时,背景似乎是白色的,并且需要一些时间才能完全加载背景。

I tested it out, it seems fine, but it seems that the background was white when I clicked on the mailto and it took some time to load the background completely.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文