如何在jquery中准备好DOM之后检查是否会发生此事件?

时间:2022-10-27 23:51:44

I want to get page_tag information from the page and want to make sure that DOM for this page is already ready before getting the page tag information.

我想从页面获取page_tag信息,并希望在获取页面标记信息之前确保此页面的DOM已准备就绪。

I am doing

我在做

$(document).ready(
{
   alert("test");
   var page_tag : $("head meta[name='page_tag']").attr('content');
   page_tag : (page_tag) ? page_tag : '';
}

But it gives me errors,

但它给了我错误,

missing : after property id
alert("Check if document is ready");\n

Any suggestions on what could be the possible reasons for it or any other way of checking if the dom is ready or not before getting page_tag information.

在获取page_tag信息之前,有关可能的原因或任何其他方式检查dom是否准备好的任何建议。

3 个解决方案

#1


4  

try

$(document).ready(function() {
   var page_tag = $("head meta[name='page_tag']").attr('content');
   alert(page_tag);
});

The ready() function requires you to pass in a function that it will execute when the document is ready.

ready()函数要求您传入一个在文档准备好时将执行的函数。

#2


2  

You have a syntax error, you are using : instead of = to make the assignment:

您有语法错误,您正在使用:而不是=来进行分配:

var page_tag = $("head meta[name='page_tag']").attr('content');
page_tag = (page_tag) ? page_tag : '';

Or simply:

var page_tag = $("head meta[name='page_tag']").attr('content') || '';

The above will work, because the attr method returns a String or undefined if the attribute is not present.

上面的方法是有效的,因为如果属性不存在,attr方法返回String或undefined。

#3


0  

I'm pretty sure it should be:

我很确定它应该是:

$(document).ready(function() {
   alert("test");
   var page_tag = $("head meta[name='page_tag']").attr('content');
   page_tag = (page_tag) ? page_tag : '';
}

You need to use a = instead of :

您需要使用a =而不是:

#1


4  

try

$(document).ready(function() {
   var page_tag = $("head meta[name='page_tag']").attr('content');
   alert(page_tag);
});

The ready() function requires you to pass in a function that it will execute when the document is ready.

ready()函数要求您传入一个在文档准备好时将执行的函数。

#2


2  

You have a syntax error, you are using : instead of = to make the assignment:

您有语法错误,您正在使用:而不是=来进行分配:

var page_tag = $("head meta[name='page_tag']").attr('content');
page_tag = (page_tag) ? page_tag : '';

Or simply:

var page_tag = $("head meta[name='page_tag']").attr('content') || '';

The above will work, because the attr method returns a String or undefined if the attribute is not present.

上面的方法是有效的,因为如果属性不存在,attr方法返回String或undefined。

#3


0  

I'm pretty sure it should be:

我很确定它应该是:

$(document).ready(function() {
   alert("test");
   var page_tag = $("head meta[name='page_tag']").attr('content');
   page_tag = (page_tag) ? page_tag : '';
}

You need to use a = instead of :

您需要使用a =而不是: