Javascript 为什么我的if条件阻止任何一个子句执行?

Javascript 为什么我的if条件阻止任何一个子句执行?,javascript,jquery,Javascript,Jquery,我正在尝试使用jQuery类选择器重新编写一组链接的URL。但是,我只想重新编写尚未指定href属性的链接,因此我加入了if/else结构来检查此。。。但是,它不起作用。它在没有if-else语句的情况下工作,所以我很确定这就是我搞砸的地方。我对JavaScript和jQuery都是新手,如果我的问题是基本的和/或过于明显的,我很抱歉 var url = window.location; var barTwitter = $("a.shareTwitter").attr('href'

我正在尝试使用jQuery类选择器重新编写一组链接的URL。但是,我只想重新编写尚未指定href属性的链接,因此我加入了if/else结构来检查此。。。但是,它不起作用。它在没有if-else语句的情况下工作,所以我很确定这就是我搞砸的地方。我对JavaScript和jQuery都是新手,如果我的问题是基本的和/或过于明显的,我很抱歉

   var url = window.location;
   var barTwitter = $("a.shareTwitter").attr('href');
   if (barTwitter).val() == "null") {
   $("a.barTwitter").attr('href','http://www.twitter.com/home?status='+ url +'');
     } else {
   $("a.barTwitter").attr('href',barTwitter);
   }
这在语法上是无效的(计算括号!)。你宁愿做:

if (barTwitter.val() == "null") {
此外,该函数仅适用于jQuery包装的输入元素,而不适用于元素属性值,这些属性值最后只是普通变量。您更希望将普通变量与文本
null
进行比较:

if (barTwitter == null) {

你的第一个语法是错误的:
if(barTwitter.val()=“null”)
应该是
if(barTwitter.val()=“null”)
或者
if((barTwitter.val()=“null”)

其次,
barTwitter
要么是字符串,要么是
null
,因此不能调用
val
,这是一种特定于
input
元素的jQuery对象方法

最后,您可能不想与null进行比较,因为该值可能是空字符串。因此,最好使用长度属性或其他方法。具有长度的样本如下所示。。但我不确定如果没有值,attr会返回什么。。。检查文档

var url = window.location;
   var barTwitter = $("a.shareTwitter").attr('href');
   if (barTwitter.length < 1) {
   $("a.barTwitter").attr('href','http://www.twitter.com/home?status='+ url +'');
     } else {
   $("a.barTwitter").attr('href',barTwitter);
   }
var url=window.location;
var barTwitter=$(“a.shareTwitter”).attr('href');
if(barTwitter.length<1){
$(“a.barTwitter”).attr('href','http://www.twitter.com/home?status=“+url+”;
}否则{
$(“a.barTwitter”).attr('href',barTwitter);
}

您的代码实际上存在一些问题。。。BALUSC-你的代码中的语法错误>如果条件-但是你应该考虑其余的一些……/P> 首先,我将根据BalusC的回答更正您的代码,并添加注释来描述发生了什么:

var url = window.location; // obtain the URL of the current document
// select the href attribute of the first <a> element with a shareTwitter class
var barTwitter = $("a.shareTwitter").attr('href');
if (barTwitter == null) { // if that attribute was not specified,
   // set the attribute of every matching element to a combination of a fixed URL
   // and the window location 
   $("a.barTwitter").attr('href','http://www.twitter.com/home?status='+ url +'');
} else {
   // set the attribute of every matching element to that of the first 
   // matching element
   $("a.barTwitter").attr('href',barTwitter);
}

请注意,尽管这段新代码完成了相同的任务,但它这样做的同时避免了一些潜在的问题并保持简洁。这就是jQuery的美妙之处……

您的代码显然是错误的(即使括号也不匹配)。重新表述您的问题会很好,这样我们就可以更好地理解您想要的内容,因为代码在这方面没有多大帮助。答案是修复语法错误的问题真让我生气。你用什么调试器吗?!你看过什么吗?!我的代码不工作,有人告诉我为什么!!!你需要学习如何自己调查类似的问题,勤奋,否则你成为一名成功的web开发人员的机会很小。简单,Josh。很多人试图在没有合适工具的情况下学习JS。。。悲伤,但这是真的。冷静一点,乔希。堆栈溢出用于询问编程问题,而不是询问专家级编程问题。你也曾经是初学者,对不起,乔希。您是否可以推荐其他工具?我在这里努力学习。谢谢啊,这就解决了。谢谢你的澄清!谢谢你提供的所有信息。我要给萤火虫打一针。我用过一两次,但有点难以承受。长话短说,如果需要if/else构造,根据我上面提供的内容,它可能没有意义,但我有理由使用它。哦,谢谢你让我知道这个组件,现在就开始吧。
var url = window.location; // obtain the URL of the current document
// select the href attribute of the first <a> element with a shareTwitter class
var barTwitter = $("a.shareTwitter").attr('href');
if (barTwitter == null) { // if that attribute was not specified,
   // set the attribute of every matching element to a combination of a fixed URL
   // and the window location 
   $("a.barTwitter").attr('href','http://www.twitter.com/home?status='+ url +'');
} else {
   // set the attribute of every matching element to that of the first 
   // matching element
   $("a.barTwitter").attr('href',barTwitter);
}
var url = window.location; // obtain the URL of the current document
// escape URL for use in a querystring
url = encodeURIComponent(url);
// select all <a> elements with a shareTwitter class and no href attribute
var twitterLinks = $("a.shareTwitter:not([href])");
// update each selected link with a new, custom link
twitterLinks.attr('href', 'http://www.twitter.com/home?status='+ url +'');