取消/禁用JavaScript中的window.location。

huangapple go评论60阅读模式
英文:

cancel/disable window.location in JavaScript

问题

<tr class="clickable-row" data-row-id="{{$Offer->offer_nr}}" data-href='link'>
$(".clickable-row").click(function() {
  window.location = $(this).data("href");
});

在我的最后一行中,每一行都有一个按钮来打开一个模态框。当我点击按钮来打开我的模态框时,如何在JavaScript中取消window.location?

我使用CSS使最后一行中的单元格不可点击:
pointer-events: none;
但是按钮也变得不可点击。

我还尝试在点击按钮时停止window.location:

$(document).on('click', '.delete-record', function (e) {
    e.preventDefault();
}
英文:

I link every row in my table with js

<tr class="clickable-row" data-row-id="{{$Offer->offer_nr}}" data-href='link'>
$(".clickable-row").click(function() {
  window.location = $(this).data("href");
});

In my last cell, in every row, I have a button to open a modal. How can I cancel the window.location in JavaScript when I click the button to open my modal?

I made the last cell in my row unclickable with CSS:
pointer-events: none;
but the button is then also unclickable

I tried also to stop the window.location when I click on the button with

 $(document).on('click', '.delete-record', function (e) {
    e.preventDefault();
 }

答案1

得分: 1

你不想阻止默认操作(对于点击表格单元格来说,默认操作是什么都不做),你想要停止事件传播jQuery):

$('.delete-record', function (e) {
    e.stopPropagation();
});

这将导致你在.clickable-row上的处理程序不会触发。

英文:

You don't want to prevent the default action (which is nothing for a click on a table cell), you want to stop the event propagation (jQuery):

$('.delete-record', function (e) {
    e.stopPropagation();
});

This will cause your handler on .clickable-row to simply not trigger.

答案2

得分: 0

你可以通过点击的目标来检测按钮是否被点击。

$(".clickable-row").click(function(e) {
  const deleteClicked = e.target.closest('.delete-record');
  if (deleteClicked) {
    // 如果需要的话,调用删除模态框
    return false;
  } 
  // window.location = $(this).data("href");
  console.log($(this).data("href"));
});

或者你可以阻止事件向下传递,但你不能在文档上使用事件代理,因为事件在到达文档时已经被行触发了。所以你需要直接绑定到按钮上。

$(".clickable-row").click(function(e) {
  // window.location = $(this).data("href");
  console.log("点击了行:", $(this).data("href"));
});

$(".delete-record").click(function(e) {
  e.stopPropagation();
  console.log("点击了按钮");
});
英文:

You can detect if the button was clicked using the target of what was clicked.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(&quot;.clickable-row&quot;).click(function(e) {
  const deleteClicked = e.target.closest(&#39;.delete-record&#39;);
  if (deleteClicked) {
    // call delete modal if you want
    return false;
  } 
  // window.location = $(this).data(&quot;href&quot;);
  console.log($(this).data(&quot;href&quot;));
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;table&gt;
  &lt;tbody&gt;
    &lt;tr class=&quot;clickable-row&quot; data-href=&quot;a&quot;&gt;
      &lt;td&gt;a&lt;/td&gt;
      &lt;td&gt;&lt;button class=&quot;delete-record&quot;&gt;X&lt;/button&gt;
    &lt;/tr&gt;
    &lt;tr class=&quot;clickable-row&quot; data-href=&quot;b&quot;&gt;
      &lt;td&gt;b&lt;/td&gt;
      &lt;td&gt;&lt;button class=&quot;delete-record&quot;&gt;X&lt;/button&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

<!-- end snippet -->

or you can stop the event from going down the tree, but you can not use event delegation on the document because the event has already been fired on the row by the time it gets to the document. So you would need to bind to the buttons directly.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(&quot;.clickable-row&quot;).click(function(e) {
  // window.location = $(this).data(&quot;href&quot;);
  console.log(&quot;clicked row:&quot;, $(this).data(&quot;href&quot;));
});

$(&quot;.delete-record&quot;).click(function(e) {
  e.stopPropagation();
  console.log(&quot;clicked button&quot;);
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;table&gt;
  &lt;tbody&gt;
    &lt;tr class=&quot;clickable-row&quot; data-href=&quot;a&quot;&gt;
      &lt;td&gt;a&lt;/td&gt;
      &lt;td&gt;&lt;button class=&quot;delete-record&quot;&gt;X&lt;/button&gt;
    &lt;/tr&gt;
    &lt;tr class=&quot;clickable-row&quot; data-href=&quot;b&quot;&gt;
      &lt;td&gt;b&lt;/td&gt;
      &lt;td&gt;&lt;button class=&quot;delete-record&quot;&gt;X&lt;/button&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

<!-- end snippet -->

答案3

得分: 0

这是一个可能的解决方案。

$('table').on('click', 'tr', function(e){
    if (e.target.tagName == "BUTTON"){
        console.log("clicked button");
    }else{
        //window.location = "whatever";
        console.log("clicked row");
    }
});

$('table button').on('click', e => {
    console.log('button');
});
td{padding:10px}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table border="1">
    <tr><td>A1</td><td>B1</td><td><button class="delete-record">click1</button></td></tr>
    <tr><td>A2</td><td>B2</td><td><button>click2</button></td></tr>
    <tr><td>A3</td><td>B3</td><td><button>click3</button></td></tr>
    <tr><td>A4</td><td>B4</td><td><button>click4</button></td></tr>
</table>
英文:

Here's a possible solution.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(&#39;table&#39;).on(&#39;click&#39;, &#39;tr&#39;, function(e){
	if (e.target.tagName == &quot;BUTTON&quot;){
  	console.log(&quot;clicked button&quot;);
  }else{
  	//window.location = &quot;whatever&quot;;
  	console.log(&quot;clicked row&quot;);
  }
	

});

$(&#39;table button&#39;).on(&#39;click&#39;, e =&gt; {
	console.log(&#39;button&#39;);

});

<!-- language: lang-css -->

td{padding:10px}

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;table border=&quot;1&quot;&gt;
&lt;tr&gt;&lt;td&gt;A1&lt;/td&gt;&lt;td&gt;B1&lt;/td&gt;&lt;td&gt;&lt;button class=&quot;delete-record&quot;&gt;click1&lt;/button&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;A2&lt;/td&gt;&lt;td&gt;B2&lt;/td&gt;&lt;td&gt;&lt;button&gt;click2&lt;/button&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;A3&lt;/td&gt;&lt;td&gt;B3&lt;/td&gt;&lt;td&gt;&lt;button&gt;click3&lt;/button&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;A4&lt;/td&gt;&lt;td&gt;B4&lt;/td&gt;&lt;td&gt;&lt;button&gt;click4&lt;/button&gt;&lt;/td&gt;&lt;/tr&gt;

&lt;/table&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月29日 21:04:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76581343.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定