You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何在HREF链接中使用JQuery实现点击触发指定操作?

Hey there! The problem with your original link is that the href attribute is meant for URLs, not raw JavaScript code. When you put that jQuery click call directly into href, the browser treats it as a literal string and tries to navigate to it instead of executing the code. Let's fix this with a couple of solid solutions:

Solution 1: Use onclick with a dummy href

You can move your jQuery code to the onclick attribute, and set href to javascript:void(0) (or # with a prevent default) to stop the page from jumping:

<a href="javascript:void(0)" onclick="$('#angrid656 > div.angrid > div:nth-child(2) > div > table > tbody > tr:nth-child(1) > td.angrid-item-buttons.ng-scope > a.btn.btn-info.btn-accent.opacity75').click()">
  <div class="title">[Title]</div>
</a>

Quick Note:

If you prefer using href="#" instead, add return false; at the end of the onclick code to prevent the page from scrolling to the top:

<a href="#" onclick="$('#angrid656 > div.angrid > div:nth-child(2) > div > table > tbody > tr:nth-child(1) > td.angrid-item-buttons.ng-scope > a.btn.btn-info.btn-accent.opacity75').click(); return false;">
  <div class="title">[Title]</div>
</a>

For cleaner, more maintainable code (and to follow best practices like separation of concerns), bind the click event in a separate script block instead of using inline attributes:

First, give your trigger link an ID:

<a href="#" id="my-trigger-link">
  <div class="title">[Title]</div>
</a>

Then add this script (make sure it runs after the DOM is ready, especially since your target element has ng-scope which suggests it's dynamically rendered by AngularJS):

// Wait for the DOM to fully load before binding events
$(document).ready(function() {
  $('#my-trigger-link').click(function(e) {
    // Prevent the default link behavior (page jump)
    e.preventDefault();
    
    // Execute your target click action
    $('#angrid656 > div.angrid > div:nth-child(2) > div > table > tbody > tr:nth-child(1) > td.angrid-item-buttons.ng-scope > a.btn.btn-info.btn-accent.opacity75').click();
  });
});

Bonus: Handling Dynamic AngularJS Elements

Since your target button has ng-scope, it’s likely rendered dynamically by AngularJS. For extra reliability, use .trigger('click') instead of .click(), and ensure your code runs after Angular has finished rendering the element:

$(document).ready(function() {
  $('#my-trigger-link').click(function(e) {
    e.preventDefault();
    // Trigger the click event explicitly for dynamic elements
    $('#angrid656 > div.angrid > div:nth-child(2) > div > table > tbody > tr:nth-child(1) > td.angrid-item-buttons.ng-scope > a.btn.btn-info.btn-accent.opacity75').trigger('click');
  });
});

If you’re working directly with AngularJS, consider adding this logic inside an Angular controller instead of raw jQuery for better framework integration.

内容的提问来源于stack exchange,提问作者UserSN

火山引擎 最新活动