如何获取 onclick 函数在 JavaScript 中被调用的位置

发布于 2025-01-04 10:08:40 字数 1198 浏览 0 评论 0原文

任何人都可以给我有关如何确定 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我的影子我的梦 2025-01-11 10:08:40

只需在函数标签中添加参数:

label(type)

并在 html 中定义类型:

 <a href="#" onClick="label('badges')">Woven Badges</a>
 <a href="#" onClick="label('tapes')">Woven tapes</a>

并在函数标签中,对每种类型进行处理。

PS:您不应在“a”元素中添加“div”元素

Just add parameter in function label :

label(type)

And in your html define type :

 <a href="#" onClick="label('badges')">Woven Badges</a>
 <a href="#" onClick="label('tapes')">Woven tapes</a>

And in your function label, do the treatment for each type.

PS : You should not add 'div' element in 'a' element

冰火雁神 2025-01-11 10:08:40

只需将 this 作为参数传递给 label() 函数即可。它是对发起事件的元素的引用。然后,您可以通过抓取第一个子元素的内部 HTML 轻松找到所需的字符串。

<script type="text/javascript">
function label(element) {
    document.getElementsByTagName("h4")[0].innerHTML=element.firstChild.innerHTML;
    return false;
}
</script>

<ul class="overview">

<li><img src="images/UniwinNav.jpg" align="texttop" /><a href="#" onclick="return label(this)"><div class="listlabel">Woven Labels</div></a></li>
<li><img src="images/UniwinNav.jpg" align="texttop" /><a href="#" onclick="return label(this)"><div class="listlabel">Woven Badges</div></a></li>
<li><img src="images/UniwinNav.jpg" align="texttop" /><a href="#" onclick="return label(this)"><div class="listlabel">Woven Tapes</div></a></li>
<li><img src="images/UniwinNav.jpg" align="texttop" /><a href="#" onclick="return label(this)"><div class="listlabel">Woven WaistBands</div></a></li>

<h4>Product Specification</h4>

请注意,最好向 label() 函数添加返回值,并在单击元素时返回它。这将取消默认的浏览器操作,这意味着您不会在您的位置看到讨厌的 # 字符。

Just pass this as an argument to the label() 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.

<script type="text/javascript">
function label(element) {
    document.getElementsByTagName("h4")[0].innerHTML=element.firstChild.innerHTML;
    return false;
}
</script>

<ul class="overview">

<li><img src="images/UniwinNav.jpg" align="texttop" /><a href="#" onclick="return label(this)"><div class="listlabel">Woven Labels</div></a></li>
<li><img src="images/UniwinNav.jpg" align="texttop" /><a href="#" onclick="return label(this)"><div class="listlabel">Woven Badges</div></a></li>
<li><img src="images/UniwinNav.jpg" align="texttop" /><a href="#" onclick="return label(this)"><div class="listlabel">Woven Tapes</div></a></li>
<li><img src="images/UniwinNav.jpg" align="texttop" /><a href="#" onclick="return label(this)"><div class="listlabel">Woven WaistBands</div></a></li>

<h4>Product Specification</h4>

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.

用心笑 2025-01-11 10:08:40

您在任何事件处理程序中都有一个上下文 - this。它绑定到与事件关联的元素。所以你可以这样做:

function my_handler(elem) {
  elem.href // contains href of anchor, eg '#Woven_WaistBands'
}

<li><img src="images/UniwinNav.jpg" align="texttop"/><a href="#Woven_Tapes" onclick="my_handler(this); return false;"><span class="listlabel">Woven Tapes</span></a></li>
<li><img src="images/UniwinNav.jpg" align="texttop"/><a href="#Woven_WaistBands" onclick="my_handler(this); return false;"><span class="listlabel">Woven WaistBands</span></a></li>

<h4 id="Woven_Tapes">blah blah</h4>
<h4 id="Woven_WaistBands">...</h4>

但最好不要重复自己,并使用 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:

function my_handler(elem) {
  elem.href // contains href of anchor, eg '#Woven_WaistBands'
}

<li><img src="images/UniwinNav.jpg" align="texttop"/><a href="#Woven_Tapes" onclick="my_handler(this); return false;"><span class="listlabel">Woven Tapes</span></a></li>
<li><img src="images/UniwinNav.jpg" align="texttop"/><a href="#Woven_WaistBands" onclick="my_handler(this); return false;"><span class="listlabel">Woven WaistBands</span></a></li>

<h4 id="Woven_Tapes">blah blah</h4>
<h4 id="Woven_WaistBands">...</h4>

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).

一曲爱恨情仇 2025-01-11 10:08:40

添加一个参数,当调用 label() 时,它将传递源。您可以传递一个字符串,如下所示,或者您可以传递锚点本身并将其与 jquery 一起使用来查找您正在修改的子项。您不必使用 jQuery,但如果您想的话,我向您展示了下面的示例。

function label(source){
    $(source).find('h4').html('badges');
};

<a href... onClick='label(this)'>

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.

function label(source){
    $(source).find('h4').html('badges');
};

<a href... onClick='label(this)'>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文