Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/470.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在单独的元素中更改javascript变量_Javascript_Html_Class_Variables_Element - Fatal编程技术网

在单独的元素中更改javascript变量

在单独的元素中更改javascript变量,javascript,html,class,variables,element,Javascript,Html,Class,Variables,Element,我运行了一个基于php phpbb、javascript和html的魔兽世界公会论坛。长期以来,Wowhead允许将链接发布到其项目/拼写ID等。Wowhead JS及其变量的基本代码是: <script src="//static.wowhead.com/widgets/power.js"></script> <script>var wowhead_tooltips = { "colorlinks": true, "iconizelinks": true,

我运行了一个基于php phpbb、javascript和html的魔兽世界公会论坛。长期以来,Wowhead允许将链接发布到其项目/拼写ID等。Wowhead JS及其变量的基本代码是:

<script src="//static.wowhead.com/widgets/power.js"></script>
<script>var wowhead_tooltips = { "colorlinks": true, "iconizelinks": true, "renamelinks": true }</script>
有一个扩展,它通过HTML文件将此代码放在每个页面的页脚中。发布的每个Wowhead链接都将转换为一个带有工具提示的链接,解释它链接到的内容。wowhead\u tooltips变量的“renamelink:true”部分使项目或拼写的任何链接都被重命名为其链接的确切名称

问题:当我使用Wowhead链接生成自定义URL时,即:

<a href="http://www.wowhead.com/spell=1953">Teleport</a>
它将使用图标将整个URL重命名为闪烁,而不是显示工具提示为闪烁的“传送”,如wowhead\u tooltips变量中所述

我想要实现的是:

任何指向Wowhead的直接URL都应转换为重命名的拼写/项目。 Wowhead的任何自定义URL都应该保留其自定义文本,但检索工具提示。 这两种方法都可以在一个页面上实现。 我提出的最佳解决方案是根据类向var wowhead_工具提示添加“if”函数,然后将类添加到URL:

<script>if ($('a').hasClass("wowrename")) { var wowhead_tooltips = { "colorlinks": true, "iconizelinks": true, "renamelinks": false } }</script>
<a class="wowrename" href="http://www.wowhead.com/spell=1953">Teleport</a>
但是,这是可行的,这个解决方案的问题是,一旦脚本在页面上识别出一个具有类wowrename的URL,它将停止重命名所有链接,这意味着自定义URL和直接URL不能在单个页面上混合

我尝试过的任何其他解决方案,使用ID,定义不同的变量等,要么不起作用,要么产生相同的限制


因此,问题是,在这种情况下,是否可以根据id、类或其他任何内容更改每个元素URL的var wowhead_工具提示{renamelinks:false}的Javascript变量?

您必须在所有链接上循环,如下所示:

$("a.wowrename").each(function() {
    // some code
});

这是一个简单的解决方案。这不是最好的办法

var wowhead_tooltips = { "colorlinks": true, "iconizelinks": true, "renamelinks": true }
$('a').hover(function() {
  if ($(this).hasClass('wowrename') {
    wowhead_tooltips.renamelinks = true;
  }
  else {
    wowhead_tooltips.renamelinks = false;
  }
});
我不知道wowhead API到底是如何工作的,但是如果wowhead_tooltips变量恰好在用户用鼠标指向链接时加载,并且没有任何超时,那么这可能会失败或随机工作/不工作。 原因可能是javascript不知道首先执行哪个函数


我希望这能奏效。如果不是-评论我会考虑另一种方式。

直接链接,使用工具提示和iccn重命名

<a href="http://www.wowhead.com/spell=1953">Teleport</a>
这确实会导致链接图标先闪烁后闪烁,但在页面刷新后会重复


我试过了,但没用。我认为本质上这不是我正在寻找的修复,因为如果我理解正确的话,这只会根据链接是否悬停而更改脚本。我希望它是如果页面被加载。实际上wowhead\u tooltips变量用于所有链接。您可以看到wowhead\u tooltip.renamelinks是复数形式。我将寻找一些只能在特定链接上设置renamelins功能的东西。我尝试了添加:codevar wowhead_tooltips={colorlinks:true,iconizelinks:true,renamelinks:false}但它似乎不起作用。。。似乎是我想要的,但不知道为什么它不工作。你建议在循环中使用什么代码?在这个循环中更改wowhead_变量是没有意义的。结果将是->property wowhead\u tooltips.renamelinks将为true所有链接的行为将为renamelinks是的,就是这样!我必须非常诚实,我确实理解这一变化的原因,但它是有效的,但我想理解:如果我掌握正确,您让脚本运行时,由于数据href无法识别直接链接,然后将data href更改为href,这样链接将保留但不会被重命名?我注意到此修复程序存在一个小问题:最初它工作正常,但当我重新加载页面时,它仍会重命名任何自定义URL。当我离开页面直接返回时,它会自动恢复。这是一个小问题,但有解决办法吗?我已经更新了我的答案,以帮助解决重新加载问题。希望它也能得到更好的解释。如果不是的话,请再问一次。我明白你对图标闪光灯的意思,但这不是问题。再次感谢!我掌握了它正在做什么的概念。如果你同意的话,我想我会指导扩展phpbb的游戏工具提示的作者,作者PayBas回答这个问题,他是否想在phpbb扩展中包含一个变体。对于那些同样对phpbb感兴趣的人:我添加了我自己的bbCode:bbCode:[wow={TEXT1}]{TEXT2}[/wow]HTML:轻微添加:bbCode HTML包含wowrenameoff类;我重命名了这个类,因为我认为它更符合逻辑,因为它阻止重命名。为了使其工作,必须将脚本中的类重命名为wowrenameoff a.wowrenameoff,或者将BBCode中的类重命名为wowrename。
<a class="wowrename" href="http://www.wowhead.com/spell=1953" data-value="Teleport">Teleport</a>
$(function () {
    //timmer
    checkChanged = setInterval(function () {
        // check for when the last link text has changed
        var lastItem = $("a.wowrenameoff").last();
        if (lastItem.text() !== lastItem.data('value')) {
            $("a.wowrenameoff").each(function () {
                //change value
                $(this).text($(this).data('value'));
                //remove icon
                $(this).attr('style', '');

                //stop timer
                clearInterval(checkChanged);
            });
        }

        i++;
    }, 100);
});