一个href =“mailto ..”破解了我的javascript

时间:2011-11-11 17:33:10

标签: javascript html javascript-events href mailto

我做了一个主页。如果您点击需要更改背景的任何地方,请点击带有联系人的图片。如果您通过联系人点击该图片,邮件应发送一个电子邮件地址。 现在正在工作,唯一的问题是当我点击图片时用“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:mail@mail.com">
            <img alt="contact" src="files/contact.png"/>
        </a>
    </div>
    <div id="footer" class="footer">
        Sample text
    </div>
</div>

感谢:科利

4 个答案:

答案 0 :(得分:2)

添加此代码:

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

答案 1 :(得分:0)

尝试此功能:

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之间的随机数,然后隐藏您想要隐藏的两个元素,并且比您的代码小得多。

如果问题在此处再次发表评论,我将再看看它。

答案 2 :(得分:0)

有点像Munim Abdul所说的尝试你可以添加以阻止传播

       <a href="mailto:mail@mail.com;" onclick="event.stopPropagation();">

答案 3 :(得分:-1)

我测试了它,看起来很好,但是当我点击mailto时背景似乎是白色的,并且花了一些时间来完全加载背景。

相关问题