如何获取 onclick 函数在 JavaScript 中被调用的位置
任何人都可以给我有关如何确定 onclick
事件被调用的位置的语法吗?
我想根据 onclick
的来源来更改 h4 内容
例如:
- 如果在“knitted badages”中调用
label()
,则 h4 insidehtml 将是“writing badages” “ - 如果在“编织带”中调用
label()
,h4 insidehtml 将是“编织带”
我想将其合并到一个函数中,而不是为每个类别创建一个函数目前我正在使用下面的 JavaScript。如何将其创建为if条件?
function label()
{ document.getElementsByTagName("h4")[0].innerHTML="badges";}
<ul class="overview">
<li><img src="images/UniwinNav.jpg" align="texttop"/><a href=""><div class="listlabel">Woven Labels</div></a></li>
<li><img src="images/UniwinNav.jpg" align="texttop"/><a href="#" onClick='label()'><div class="listlabel">Woven Badges</div></a></li>
<li><img src="images/UniwinNav.jpg" align="texttop"/><a href=""><div class="listlabel">Woven Tapes</div></a></li>
<li><img src="images/UniwinNav.jpg" align="texttop"/><a href=""><div class="listlabel">Woven WaistBands</div></a></li>
<h4>Product Specification</h4>
</ul>
Can anybody give me the syntax on how could I determine where the onclick
event was called.
I want to change the h4 content depending on where the onclick
came from
For example:
- if
label()
is called in "woven badges" , h4 innerhtml will be "woven badges" - if
label()
is called in "woven tapes" , h4 innerhtml will be "woven tapes"
I want to incorporate this into just one function and not to actually create one function per category which I'm currently doing with my JavaScript below. How can create it into if conditions?
function label()
{ document.getElementsByTagName("h4")[0].innerHTML="badges";}
<ul class="overview">
<li><img src="images/UniwinNav.jpg" align="texttop"/><a href=""><div class="listlabel">Woven Labels</div></a></li>
<li><img src="images/UniwinNav.jpg" align="texttop"/><a href="#" onClick='label()'><div class="listlabel">Woven Badges</div></a></li>
<li><img src="images/UniwinNav.jpg" align="texttop"/><a href=""><div class="listlabel">Woven Tapes</div></a></li>
<li><img src="images/UniwinNav.jpg" align="texttop"/><a href=""><div class="listlabel">Woven WaistBands</div></a></li>
<h4>Product Specification</h4>
</ul>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只需在函数标签中添加参数:
并在 html 中定义类型:
并在函数标签中,对每种类型进行处理。
PS:您不应在“a”元素中添加“div”元素
Just add parameter in function label :
And in your html define type :
And in your function label, do the treatment for each type.
PS : You should not add 'div' element in 'a' element
只需将
this
作为参数传递给label()
函数即可。它是对发起事件的元素的引用。然后,您可以通过抓取第一个子元素的内部 HTML 轻松找到所需的字符串。请注意,最好向
label()
函数添加返回值,并在单击元素时返回它。这将取消默认的浏览器操作,这意味着您不会在您的位置看到讨厌的#
字符。Just pass
this
as an argument to thelabel()
function. It is a reference to the element that initiated the event. You can then easily find the string you want by grabbing the inner HTML of the first child element.Note that it is a good idea to add a return value to the
label()
function and return it when the element is clicked. This will cancel the default browser action, meaning you don't get that pesky#
character in your location.您在任何事件处理程序中都有一个上下文 -
this
。它绑定到与事件关联的元素。所以你可以这样做:但最好不要重复自己,并使用 jQuery 或本机
addEventListenet
将处理程序绑定到事件(由于 IE,目前这种方式不太便携)。You have a context -
this
- in any event handler. Its bound to the element associated to the event. So you can do smth like this:But it is better not to repeat your self and bind handlers to events using jQuery or native
addEventListenet
(that is less portable way for now because of IE).添加一个参数,当调用 label() 时,它将传递源。您可以传递一个字符串,如下所示,或者您可以传递锚点本身并将其与 jquery 一起使用来查找您正在修改的子项。您不必使用 jQuery,但如果您想的话,我向您展示了下面的示例。
Add a parameter, and when label() is called it will pass the source. You could pass a string as below, or you could pass the anchor itself and use it with jquery to find the children you are modifying. you don't have to use jQuery, but in case you want to I showed you an example below.