简体   繁体   中英

How to find and replace href URL with # Hashtag under Table dive ID

Hi I'm trying to replace href URL with the # hashtag

<td data-label="Name">
  <a href="https://mysite . com/" style="display: block;"> cabinet 1 </a>
  <div class="wbs-select">
    <select id="variations" class="wbs-variation-select" name="attribute_variations" data-attribute_name="attribute_variations" data-show_option_none="yes">
      <option value="">Select Variations</option>
      <option value="RTA">RTA</option>
      <option value="Assembled">Assembled</option>
    </select>
  </div>
  <span id="11-stock-status">In stock</span>
</td>

JS

$(document).ready(function () {
  $('a[href^="http://"]').each(function () {
    var oldUrl = $(this).attr("href"); // Get current url
    var newUrl = oldUrl.replace("http://", "#"); // Create new url
    $(this).attr("href", newUrl); // Set herf value
  });
});

However current script only replace http:// with #, but what I looking

  1. replace entire URL with # tag
  2. and under the table not entire page or by underclass or ID
  1. Point 1:- Just set the href value to "#"
  2. Point 2:- Wrap your anchor inside a container div and in your jQuery append the container div id like below, so that it will only find anchor list which are under that container div.

 $(document).ready(function(){ $('#main a[href^="http://"]').each(function(){ $(this).attr("href", "#"); // Set herf value }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id ="main"> <a href="http://google.com">google</a> <a href="http://asd.com">google</a> </div> <a href="http://ggg.com">google</a>

 $(document).ready(function() { for (var i = 0; i < $('a').length; i++) { if ($('a')[i].href.indexOf("http://") == 0 || $('a')[i].href.indexOf("https://") == 0) { $(this).attr("href", "#"); } } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <td data-label="Name"> <a href="https://example.com/" style="display: block;"> cabinet 1 </a> <div class="wbs-select"> <select id="variations" class="wbs-variation-select" name="attribute_variations" data-attribute_name="attribute_variations" data-show_option_none="yes"> <option value="">Select Variations</option> <option value="RTA">RTA</option> <option value="Assembled">Assembled</option> </select> </div> <span id="11-stock-status">In stock</span> </td>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM