Javascript 如何在脚本标记onload中插入文本锚?

Javascript 如何在脚本标记onload中插入文本锚?,javascript,jquery,Javascript,Jquery,如何在脚本标记中插入文本锚1作为src值的一部分?我将在选项卡式内容上使用它。谢谢 给你 $('ul.tabmenu li').click(function() { var tab_id = $(this).attr('data-tab'); $('ul.tabmenu li').removeClass('active'); $('.tab').removeClass('active'); $(this).addClass('active'); //

如何在脚本标记中插入文本锚1作为src值的一部分?我将在选项卡式内容上使用它。谢谢


给你

$('ul.tabmenu li').click(function() {
    var tab_id = $(this).attr('data-tab');
    $('ul.tabmenu li').removeClass('active');
    $('.tab').removeClass('active');
    $(this).addClass('active');

    // -- Added --
    // Grab the tab text and trim any spaces
    var tab_text = $.trim($(this).text());
    // Find the script tag
    var $script = $('#' + tab_id).find("script");
    // Replace anything b/w /-/ and ? with the tab text
    $script.attr("src", $script.attr("src").replace(/\/-\/.*?\?/gi, "/-/" + tab_text + "?"));
    // -- End --

    $('#' + tab_id).fadeIn('slow').addClass('active');
});

演示@

你到底想要什么??我已经更新了小提琴,因为脚本没有正确包括。谢谢!在我的提琴上,标题“CATEGORY 1”是tab1中javascript的一部分。等等……好吧。。所以它实际上很有帮助,对吗?你需要在锚定标签上添加锚定标签?也许我的解释不清楚。。。我更新了小提琴:)它似乎不起作用:)要替换的文本还是在/和之间?应该替换为li的文本锚。它在我发布的小提琴中确实起作用,但只有当你点击标签时才起作用。你想让它在加载时也工作吗?是的,也许我们也可以试试onload。然后替换上一个
})
})。单击()我用Inspect元素检查了它,我可以看到它在工作。也许博主就是不支持。谢谢:)
$('ul.tabmenu li').click(function() {
    var tab_id = $(this).attr('data-tab');
    $('ul.tabmenu li').removeClass('active');
    $('.tab').removeClass('active');
    $(this).addClass('active');

    // -- Added --
    // Grab the tab text and trim any spaces
    var tab_text = $.trim($(this).text());
    // Find the script tag
    var $script = $('#' + tab_id).find("script");
    // Replace anything b/w /-/ and ? with the tab text
    $script.attr("src", $script.attr("src").replace(/\/-\/.*?\?/gi, "/-/" + tab_text + "?"));
    // -- End --

    $('#' + tab_id).fadeIn('slow').addClass('active');
});