简体   繁体   English

Javascript,为什么我的onclick功能不起作用

[英]Javascript, why wont my onclick function work

Returning to JavaScript after a long break and I can't get this to work. 经过长时间的休息后回到JavaScript,我无法让它工作。 I must be doing thing really stupid because it works fine with an onclick('') attached to the button in the HTML file just not when I try and do it in the script.js 我必须做一件非常愚蠢的事情,因为它在HTML文件中附加到按钮的onclick('')工作正常,而不是在我尝试在script.js中执行时

I've tried to include everything that should be needed but if i've missed something let me know. 我试图包括所有应该需要的东西但如果我错过了什么让我知道。

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"</script>
<script type="text/javascript" src="script.js"></script>


<div class="application">
    <button type="button" class="solution" id="webSolution">
        <p>
    Website Solutions
    </p>
</button>
<button type="button" class="solution" id="mobileSolution">
    <p>
    Mobile Solutions
    </p>
</button>
</div>

<div class="mobileSolutionDetails">
<h3>
Mobile Solution
</h3>
price details
</div>
<div class="webSolutionDetails">
<h3>
Website Solution
</h3>
price details
</div>

and my javascript 和我的javascript

$(document).ready(function() {
$('#webSolution').onclick(function() {

    alert('webSolution');
    $('.webSolutionDetails').style.display = 'block';
    $('.mobileSolutionDetails').style.display = 'none';

});

$('#mobileSolution').onclick(function() {

    alert('Hey!');
    $('.mobileSolutionDetails').style.display = 'block';
    $('.webSolutionDetails').style.display = 'none';

});
});

Any help would be great 任何帮助都会很棒

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript" src="script.js"></script>

Your script must come after jquery.min.js , otherwise $ won't be defined when you try to use it. 您的脚本必须在jquery.min.js之后,否则在您尝试使用它时将不会定义$ You're also missing the end > in the jQuery script tag. 您还缺少jQuery脚本标记中的end >

It doesnt like onclick for one. 它不喜欢onclick为一个。 Changed to: 变成:

.on("click", function(){});

instead of 代替

.onclick(function(){})

if you wanted to do it your way, you would want to do 如果你想按自己的方式去做,你会想做

.click(function(){});

on a similar note, your CSS tags are wrong for jquery. 在类似的说明中,你的CSS标签对于jquery是错误的。

use: 采用:

$(item).css({display:"block"}); //instead

Here is a fiddle. 这是一个小提琴。 http://jsfiddle.net/qMsm2/ http://jsfiddle.net/qMsm2/

jquery uses "click" as function. jquery使用“click”作为函数。

http://api.jquery.com/click/ http://api.jquery.com/click/

You should use it like this: 你应该像这样使用它:

$('#webSolution').click(function() {

});

Check this fiddle: http://jsfiddle.net/4CNML/ 检查这个小提琴: http//jsfiddle.net/4CNML/

Two ways for you to write it. 你有两种方式来写它。

With .on (this is becoming the most favored way it seems) 随着.on(这似乎成为最受青睐的方式)

$('#mobileSolution').on('click', function() {
    alert('Hey!');
    $('.mobileSolutionDetails')..css({display: 'block'});
    $('.webSolutionDetails').css({display: 'none'});
});

or with .click 或者使用.click

$('#mobileSolution').click(function() {
    alert('Hey!');
    $('.mobileSolutionDetails').css({display: 'none'});
    $('.webSolutionDetails').css({display: 'none'});
});

Also, since your using jQuery the .style is invalid. 此外,由于您使用jQuery,.style无效。 Use .css like I did above. 像我上面那样使用.css。

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

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