JQuery只附加给孩子

时间:2017-03-10 09:46:06

标签: jquery userscripts

不知何故,这个用户脚本似乎将属性附加到父级而不是子级:c

请帮助我理解为什么要这样做:o

// ==UserScript==
// @name        GMod Cinema Porn
// @namespace   Bluscream
// @version     1.0
// @description H3H3
// @author      Bluscream
// @include     https://www.youtube.com/embed/OiMTuwS3xvg*
// @include     http://cinema.pixeltailgames.com/search.html
// @include     *
// @grant       unsafeWindow
// @grant       GM_xmlhttpRequest
// @require     https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js
// @run-at      document-end
// ==/UserScript==
(function() {
    'use strict';
    if (window.location.href.startsWith("https://www.youtube.com/embed/OiMTuwS3xvg")) {
        window.location.href = "http://embed.redtube.com/player/?id=42019&autostart=true";
    } else if (window.location.href == "http://cinema.pixeltailgames.com/search.html"){
        $ = jQuery || $;
        $( document ).ready(function() {
            temop = $('#content-container').children('div[style="text-align:center;font-size:0;"]:first');
            temop.append('<service>').css('background-image', 'url("http://cdn1-www.craveonline.com/assets/uploads/2013/12/man_file_1048340_youporn-icon.png")').click(function () {
                window.selectService("YouPorn", "http:\/\/www.youporn.com");
            }).hover(function () {
                window.hoverService();
            });
        });
        //$('#content-container>div[style="text-align:center;font-size:0;"]').append('<service style=\'background-image:url(\"logos\/youtube.png\")\' onclick=\'selectService(\"YouTube\",\"http:\/\/www.youtube.com\")\' onmouseover="hoverService()">-</service>');
    }
})();

1 个答案:

答案 0 :(得分:1)

因为jQuery元素是可链接的,所以使用.append()将返回前一个元素,而不是刚刚附加的元素。

在应用属性&amp;之前,需要将目标元素作为目标。事件听众;像这样的东西:

temop
  .append('<service>')
  .find('service:last')
  .css(/* ... */)
  .click(/* ... */)
  .hover(/* ... */);