我正在开发Chrome扩展程序,并且需要一种功能,以便我希望尽快(在加载之前)获取所有可见图像,隐藏它们并设置一些属性。我一直在尝试这个:
$(document).ready(function () {
$('img:visible').each(function() {
$(this).css('visibility', 'hidden');
$(this).attr('data-internalid', guid());
});
});
但是在调试时,我注意到它甚至没有迭代循环。我在这里失踪了什么?
答案 0 :(得分:2)
所以,正如我在评论中提到的那样
如果元素占用文档中的空间,则认为元素是可见的。可见元素的宽度或高度大于零。
因此,您的一个选择是使用
$(window).on('load', function() { ... });
您也可以尝试替代方案,例如以下内容。
display:none;
设置为CSS中的特定类。希望很清楚:)
答案 1 :(得分:-1)
/**
* Background Async Task to Get complete product details
*/
class GetProductDetails extends AsyncTask<String, String, String> {
JSONObject json = null;
/**
* Before starting background thread Show Progress Dialog
*/
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(EditProductActivity.this);
pDialog.setMessage("Loading product details. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Getting product details in background thread
*/
protected String doInBackground(String... params) {
// Check for success tag
int success;
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("pid", pid));
// getting product details by making HTTP request
// Note that product details url will use GET request
json = jsonParser2.makeHttpRequest(
url_product_detials, "GET", params);
// check your log for json response
Log.d("Single Product Details", json.toString());
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
**/
protected void onPostExecute(String file_url) {
// dismiss the dialog once got all details
pDialog.dismiss();
if (json != null) {
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully received product details
JSONArray productObj = json
.getJSONArray(TAG_PRODUCT); // JSON Array
// get first product object from JSON Array
JSONObject product = productObj.getJSONObject(0);
// product with this pid found
// Edit Text
txtName = (EditText) findViewById(R.id.inputName);
txtPrice = (EditText) findViewById(R.id.inputPrice);
txtDesc = (EditText) findViewById(R.id.inputDesc);
// display product data in EditText
txtName.setText(product.getString(TAG_NAME));
txtPrice.setText(product.getString(TAG_PRICE));
txtDesc.setText(product.getString(TAG_DESCRIPTION));
} else {
// product with pid not found
}
}
}
}
问题在于你的guid()函数。这个代码在firefox和chrome上工作正常。请检查函数。如果问题没有解决,那么更新你的jquery如果它是脱机的,请提供guid()函数。
$(document).ready(function (index) {
$('img:visible').each(function() {
$(this).css('visibility', 'hidden');
$(this).attr('data-internalid', "test"); /*instead of guid().I think that function have some problem.Make sure it is defined or loaded properly*/
});
});
$(function(){
//$("#btn").click(function(){
$('img:visible').each(function() {
$(this).css('visibility', 'hidden');
$(this).attr('data-internalid', "test");
});
// });
});
img{
width:100px;
height:100px;
margin:10px
}
span{
display:block;
cursor:pointer;
}
答案 2 :(得分:-3)
检查返回$(this)
的内容,
使用控制台日志。