The first time you click the link, the openSolution
function is executed.(第一次单击链接时,将执行openSolution
功能。)
That function binds the click
event handler to the link, but it won't execute it.(该函数将click
事件处理程序绑定到链接,但它不会执行它。) The second time you click the link, the click
event handler will be executed.(第二次单击该链接时,将执行click
事件处理程序。)
What you are doing seems to kind of defeat the point of using jQuery in the first place.(你正在做的事情似乎首先打败了使用jQuery的重点。) Why not just bind the click event to the elements in the first place:(为什么不直接将click事件绑定到元素:)
$(document).ready(function() {
$("#solTitle a").click(function() {
//Do stuff when clicked
});
});
This way you don't need onClick
attributes on your elements.(这样,您就不需要元素上的onClick
属性。)
It also looks like you have multiple elements with the same id
value ("solTitle"), which is invalid.(看起来你有多个具有相同id
值的元素(“solTitle”),这是无效的。) You would need to find some other common characteristic ( class
is usually a good option).(您需要找到一些其他常见特征( class
通常是一个很好的选择)。) If you change all occurrences of id="solTitle"
to class="solTitle"
, you can then use a class selector:(如果将所有出现的id="solTitle"
更改为class="solTitle"
,则可以使用类选择器:)
$(".solTitle a")
Since duplicate id
values is invalid, the code will not work as expected when facing multiple copies of the same id
.(由于重复的id
值无效,因此当面对同一个id
多个副本时,代码将无法按预期工作。) What tends to happen is that the first occurrence of the element with that id
is used, and all others are ignored.(容易发生的是使用具有该id
的元素的第一次出现,并且忽略所有其他元素。) 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…