开发者

How to add a html <a> tag with javascript or jquery?

开发者 https://www.devze.com 2023-02-21 21:28 出处:网络
I\'ve done a slideshow for news on a website which works with jquery. Everything works perfectly except for a line where I try to add a link which links the complete version of the news. But Javascrip

I've done a slideshow for news on a website which works with jquery. Everything works perfectly except for a line where I try to add a link which links the complete version of the news. But Javascript never adds the tag nor show an error on the console. Here's the function which changes the currently displayed news on the slideshow :

fun开发者_StackOverflowction changeNews(newCurrentNewsNumber)
    {
        var realNewsNumber = newCurrentNewsNumber - 1;
        $("#number" + currentNewsNumber).css("color", "#FFFFFF");
        $("#number" + currentNewsNumber).css("backgroundColor", "#474747");
        $("#imageNews").attr("src").replace(newsImagePathArray[newCurrentNewsNumber - 1]);
        $("#number" + newCurrentNewsNumber).css("color", "#055c94");
        $("#number" + newCurrentNewsNumber).css("backgroundColor", "#FFFFFF");
        $("#textNews").html(newsTitleArray[realNewsNumber]+"<br />" + newsTextArray[realNewsNumber]+"<br />");
        $("#textNews").append("<a href=\"index.php?corps=news&id="+realNewsNumber+">Voir la suite de la news...</a>");
        currentNewsNumber = newCurrentNewsNumber;
    }

newCurrentNewsNumber is the new news to display. currentNewsNumber is the old one. newsXXXArray contains elements of the news. The line in blod is the one which doesn't do what it should. Any help would appreciated.


Simply:

$("#textNews").append("<a href=\"index.php?corps=news&id="+realNewsNumber+"\">Voir la suite de la news...</a>"); 

You just forgot to add an escaped " after '+realNewsNumber+"' The devil is in the details


This works fine for me in JSFiddle:

function changeNews(newCurrentNewsNumber) {

    var realNewsNumber = newCurrentNewsNumber - 1;
    $("#textNews").html("Test Title"+"<br />" + "Text Text" +"<br />");
    $("#textNews").append("<a href=\"http://www.google.com?corps=news&id="+realNewsNumber+"\">Voir la suite de la news...</a>");
}

$(function() { changeNews(1); });

The only thing I can think of is you're trying to append the code before the DOM is completely loaded?

0

精彩评论

暂无评论...
验证码 换一张
取 消