Include link if first element

Asked

Viewed 153 times

2

I have a menu with four items. The menu is manageable, not manual via HTML.

I want to modify a link in the first menu item, I have how to do this with Jquery? I don’t even know how to search to do this.

For example:

<li class='classe' onclick='window.location=\"/paginaSYS\"'><a href='/paginaSYS'>tituloSYS</a></li>

With this top code, and with the manager I can generate the four menu items, but I want to insert a link when the user click on the first item.

I want when the user click on the first menu item, it does not open the html generated by the manager, but rather a URL that I sort.

If he clicks on ITEM 1 - Do not upload to the page generated by the manager. www.site.com/menu1 I want you to send to an address I wish.

I thought I’d make a redirect.

  • Be a little more specific. Do you want to add 1 link in 1 text first that comes, or do you want to add 1 item first from the list? Ex: I can create a <a> and leave it as the first from the list or take the first<a> from the list and add the href="what you want" attribute even if href="" is empty at the beginning of the element.

  • I don’t know if just for me, but I think your doubt has not become clear enough. I could edit and try to be more specific by kindness?

  • I just finished editing, tell me if it’s better to understand now.

3 answers

4

Maybe something like that:

// Delega um evento de clique para a primeira âncora do menu mesmo que tenha sido criada após o primeiro carregamento do DOM.
$(document).on('click', '#menu a:first', function(event) {

    // Previne que a ação padrão da âncora (O redirecionamento) seja executada.
    event.preventDefault();

    // Redireciona o documento atual para outro URL.
    document.location = 'http://example.com';

});
  • It’s not working yet. I created an ID called #first, just to get the first element with Jquery you passed.

  • Maybe adapting the selector helps... You can paste a piece of the HTML structure from the menu to make it easy?

  • Hi. This is HTML. <li class='paginapadraoMenuBt paginapadraoMenuDupSel paginapadraoMenuDup' onclick='window.Location="/paginaSYSurlSYS"'><a href='/paginaSYSurlSYS'>tituloSYS</a></li>

3

Example: http://jsfiddle.net/uASJ9/

I did it this way:

    $(document).ready(function(){
    $( "#menu a:first" ).attr( "href", "http://answall.com" );
});
  • I did this: <li id='first' class='paginapadraoMenuDup' onclick='window.Location="/paginaSYSurlSYS"'><a href='/paginaSYSurlSYS'>tituloSYS</a></li> E in Jquery $( "#first a:first" ).attr( "href", "http://www.google.com/" ); but it didn’t work

  • I don’t know why you have an onclick, don’t use events called inline. Prefer to use jQuery itself. Take a look here, I did with ul and read for you: http://jsfiddle.net/uASJ9/1/

  • good Ronny, simple!

2

I managed to solve with the following code:

$(".paginapadraoMenuBt:first").attr( "onclick", "window.open('http://www.google.com')");
  • Argh! Why replace the attribute? I would use Diego’s solution.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.