jQuery如果对象存在 [英] jQuery if object is present

查看:78
本文介绍了jQuery如果对象存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本可以复制图像的ALT文本中的文本并制作一个标题框,然后输入文字。

I have a script that copies the text from an image's ALT-text and makes a caption box, and inputs the text.

var alt = $("#hoejre p span img").attr("alt");
$('#hoejre p span img').after('<span></span>');
$('#hoejre p span span').html('<strong>' + alt.replace('-', '</strong> <em>') + '</em>');

当有图像时,这很有用。但是当没有任何图像时,脚本在IE7中失败。

This works great, when there IS an image. But when there aren't any images, the script fails in IE7.

如何用IF句子包装VAR。

How can I wrap the VAR with an IF sentence.

推荐答案

为什么在看到长度属性时调用方法?

why call a method when you could just look at length property?

if ($imgs.length) {
  ... /* one or more images exist */
}

这篇关于jQuery如果对象存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆