javascript无法使用LI标记 [英] javascript doesn't work with LI tag

查看:192
本文介绍了javascript无法使用LI标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此代码

<ul>
<input type="button" onclick="appear()"/>
<li id="addQuestionChoices">roma</li>
</ul>



css代码



css code

#addQuestionChoices{display:none;}



javascript代码



javascript code

function appear()
{document.getElementById('addQuestionChoices').style.display="block";}

但是当我按下按钮,没有什么发生,是javascript不能使用LI标签?或者是什么 ?
谢谢回答

but when i press the button , nothing happend, is javascript doesn't work with LI tag ? or what ? thank you for answering

推荐答案

在HTML中嵌入JavaScript调用是不好的做法。当功能,样式和标记保持分离时,它使代码更可维护。其次,您的< li> 元素应嵌套在< ul> c>< ol> 标签。

It's bad practice to embed JavaScript calls within HTML. It makes the code much more maintainable when the functionality, style and markup are kept seperate. Secondly your <li> element should be nested within either a pair of <ul> or <ol> tags.

我写了一个jsFiddle例子来解决这个问题:

I have written a jsFiddle example of how you could tackle this task:

http://jsfiddle.net/dLqja/1/

在这段代码中,我创建了一个点击监听器,这是通过其id附加到您的按钮。

In this code I have created a 'click' listener, this is attached to your button via its id. Upon the button press it triggers an anonymous callback function which dynamically changes the display style of your 'li' element.

包含jQuery

以下是您页面中包含的第一个JavaScript。

Make the following is the first JavaScript that you include in your page.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

这个jQuery脚本由Google托管,它有它的优点例如(它可能已经缓存在客户端浏览器访问以前的网站使用它)。

This jQuery script is hosted by Google, which has its advantages such as (it's probably already cached in the clients browser from visiting a previous website using it).

使用jQuery功能的任何JavaScript代码应该包含在上述脚本之后。

Any JavaScript code which you write which uses the functionality of jQuery should be included after the above script.

您可以通过向按钮分配事件侦听器来实现与上述类似的结果。这种方法优于使用onclick =...作为坚持从标记分离功能的规则。如果没有这些答案工作,您应该检查您的浏览器控制台的错误消息。
http://jsfiddle.net/SvufY/1/

You can achieve a similar result as the above by assigning an event listener to the button. This approach is preferable to using onclick="..." as sticks to the rule of seperating functionality from markup. If none of these answers work you should check your browsers console for error messages. http://jsfiddle.net/SvufY/1/

这篇关于javascript无法使用LI标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆