简体   繁体   English

请帮助我修复此Jquery函数并找出后备情况,以防Javascript被禁用

[英]Please help me fix this Jquery function and figure out a fallback in case Javascript is disabled

This code is supposed to get the id from a clicked anchor tag in the middle of a ul with the id 'links' and then use that id for other purposes across the page, however the alerts have shown me that I keep getting 'undefined' as if the element has no id. 该代码应该从ul中间单击的锚标签中获取ID为“ links”的ID,然后将该ID用于整个页面的其他目的,但是警报显示我一直在获取“ undefined”好像该元素没有id。 I double check my source code and they do indeed have ids, so I'm not sure what the problem is. 我仔细检查了我的源代码,它们确实有ID,所以我不确定是什么问题。

$(function() {      
    $('ul#links > li > a').click(function() {

        var planet = $(this).id;
        alert(planet);
        $('#planet').removeClass();
        $('#planet').addClass('planet');

        var bg = 'black url(../img/' + planet + '.jpg) fixed no-repeat center';
        alert(bg);
        $('body').css('background', bg);
    });             
});

Also this Jquery function is being used as a Javascript way of handling this kind of situation. 同样,此Jquery函数也被用作处理这种情况的Javascript方法。 I already had implemented a PHP solution which involved being redirected to a quick script that sets a cookie and returns the user to the previous page, then using the cookie to load a new CSS. 我已经实现了一个PHP解决方案,其中涉及将其重定向到一个快速脚本,该脚本设置一个cookie,并将用户返回到上一页,然后使用该cookie加载新的CSS。 Is there a way I could use that method as a backup to the JS method in the case that JS is disabled? 在禁用JS的情况下,是否可以使用该方法作为JS方法的备份?

The page - http://schnell.dreamhosters.com/folio/ 页面-http://schnell.dreamhosters.com/folio/

You were pretty close: 您非常接近:

$(function() {      
  $('ul#links > li > a').click(function() {
    var planet = this.id;
    $('#planet').removeClass().addClass("plane");
    var bg = 'black url(../img/' + planet + '.jpg) fixed no-repeat center';
    $('body').css('background', bg);
    return false;
  });             
});

Firstly, this.id will get you the ID. 首先, this.id将为您获取ID。 The jQuery alternative is $(this).attr("id") . jQuery的替代方案是$(this).attr("id")

Second, you can chain addClass() and removeClass() to save looking up the element twice. 其次,您可以链接addClass()removeClass()来保存两次查找元素。 This isn't necessary for your problem but is good practice. 这对于您的问题不是必需的,而是一种很好的做法。

Third, unless you want your link to traverse somewhere you need to either prevent the default action (by calling evt.preventDefault() on the event object passed to the callback) or simply ending with return false; 第三,除非您希望链接遍历某处,否则您需要阻止默认操作(通过在传递给回调的事件对象上调用evt.preventDefault() )或简单地以return false;结尾return false; (which also does evt.stopPropagation() ). (它也支持evt.stopPropagation() )。

Lastly, what you're doing is well-suited to working with Javascript disabled because you can go to a page that does the same thing. 最后,您正在执行的操作非常适合禁用Javascript,因为您可以转到执行相同操作的页面。 I wouldn't bother with setting the cookie and redirecting. 我不会理会设置cookie和重定向。 If the script name URI is /view/planet.php do this: 如果脚本名称URI为/view/planet.php请执行以下操作:

<a id="saturn" href="/view/planet.php?planet=saturn">Saturn</a>

Inside the same script you can do (I'm assuming PHP here; I didn't see any mention of what you use): 在相同的脚本中,您可以执行操作(我在这里假设使用PHP;我没有提到您使用的功能):

$planets = array(...);
$planet = $_GET['planet'];
if (in_array($planet, $planets)) {
  echo <<<END
<style type="text/css">
body { background: black url(.../img/$planet.jpg) fixed no-repeat center; }
</style>
END;
}

while rendering the page. 呈现页面时。

You are creating a jQuery object from the element and try to get the id from that. 您正在根据元素创建一个jQuery对象,然后尝试从中获取ID。 It would would if you use the attr method, but you should just get the id from the element itself: 如果您使用attr方法,那就会这样,但是您应该只从元素本身获取ID:

var planet = this.id;

As for the backup method, that sounds plausible. 至于备份方法,这听起来很合理。 However, instead of linking to a different page I would link to the same page with a querystring that determines what to show. 但是,不是链接到其他页面,而是使用确定显示内容的查询字符串链接到同一页面。

$(this).id是问题所在...使用$(this).attr('id')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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