javascript - 我可以将jquery代码放在一堆纯javascript代码中吗?

标签 javascript jquery ajax

是否可以将我的jquery代码插入到一堆javascript代码中

我想将 $ajax jquery 代码放入 function getInfo() 但似乎不起作用。我尝试用 document.ready 包装所有 js 代码 但也不起作用。

window.fbAsyncInit = function() {
  FB.init({
    appId: '201637766943985',
    cookie: true,
    xfbml: true,
    version: 'v2.8'
  });
  FB.AppEvents.logPageView();
  FB.getLoginStatus(function(response) {
    if (response.status === 'connected') {
      document.getElementById('status').innerHTML = 'We are connected.';
      document.getElementById('fb_login').style.visibility = 'hidden';
    } else if (response.status === 'not_authorized') {
      document.getElementById('status').innerHTML = 'We are not logged in.';
    } else {
      document.getElementById('status').innerHTML = 'You are not logged into Facebook';
    }
  });
};

(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) {
    return;
  }
  js = d.createElement(s);
  js.id = id;
  js.src = "//connect.facebook.net/en_US/sdk.js";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

function fb_login() {
  FB.login(function(response) {
    if (response.status === 'connected') {
      document.getElementById('status').innerHTML = 'We are connected.';
      document.getElementById('fb_login').style.visibility = 'hidden';
    } else if (response.status === 'not_authorized') {
      document.getElementById('status').innerHTML = 'We are not logged in.';
    } else {
      document.getElementById('status').innerHTML = 'You are not logged into Facebook';
    }
  }, {
    scope: 'email'
  });
}
function getInfo() {
  FB.api('/me', 'GET', {
    fields: 'first_name,last_name,name,id,email'
  }, function(response) {
    ////////call jquery AJAX here/////////////////////
  });
}

function fb_logout() {
  FB.logout(function() {
    document.location.reload();
  });
}

最佳答案

由于 status 是一个 ID,因此您需要将 jquery 编写为

$("#status").html("Hello <b>world!</b>");

jquery 中的选择器以 # 开头(对于 id),以 . 开头(对于类)。

请引用这个简单的链接以了解如何使用选择器

http://www.w3schools.com/jquery/jquery_ref_selectors.asp

关于javascript - 我可以将jquery代码放在一堆纯javascript代码中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40393810/

相关文章:

javascript - Jquery一次将多个选择器插入一个对象

javascript - 基本的 jQuery 和 PHP Ajax 表单失败

javascript - 正则表达式只捕获第一行

javascript - 在 AngularJs 中使用一些参数初始化模块

Javascript 在循环中第二次执行函数时抛出未定义的错误

javascript - 试图在返回 false 时停止函数

javascript - 从外部 Controller 设置 Angular 复选框重复

javascript - 如何在页面调整大小时保持 div 可见?

c# - 无法定位特定 div 以填充来自 jQuery AJAX ASP.NET 的数据

javascript - 从同一网站的外部页面加载 div 中的内容