我有动态添加a jQuery函数,当我想使用这个元素作为模态我不能,没有任何反应。
当我使用php添加相同的元素时,一切正常。
我的猜测是jQuery.modal没有看到这个元素,有没有办法解决这个问题?
我的功能看起来像这样:
$.each(respJSON, function(){ $('#poll-questions').append( $('<li>').append( $('<a>').attr('href', prefix + this.id).attr('class', 'modal') .attr('rel', '{handler: "iframe", size:{x:600, y:500}}') .append($('<span>').append(this.question) ))); });I have jQuery function that adds a dynamicly, and when i want to use this element as modal i can't, nothing happens.
When i add the same element using php everything is working fine.
My guess is that jQuery.modal don't see this element, is there way to fix this?
My function look like this:
$.each(respJSON, function(){ $('#poll-questions').append( $('<li>').append( $('<a>').attr('href', prefix + this.id).attr('class', 'modal') .attr('rel', '{handler: "iframe", size:{x:600, y:500}}') .append($('<span>').append(this.question) ))); });最满意答案
确保只导入1个jQuery库。 如果您有多个,请使用以下代码检查是否已导入库。 如果有,请不要包含您自己的,如果没有,请包含您自己的:
<?php // load jQuery, if not loaded before if(!JFactory::getApplication()->get('jquery')){ JFactory::getApplication()->set('jquery',true); $document = JFactory::getDocument(); $document->addScript(JURI::root() . "templates/template_name/js/jquery-1.8.3.js"); } ?>显然改变路径,以满足您的需求。
如果这没有帮助,请确保您已正确添加了view.html.php文件的代码,如下所示:
$doc = JFactory::getDocument(); //remove this line if it's already in the file $js = '$.each(respJSON, function(){ $("#poll-questions").append( $('<li>').append( $("<a>").attr("href", prefix + this.id).attr("class", "modal") .attr("rel", "{handler: "iframe", size:{x:600, y:500}}") .append($("<span>").append(this.question) )); });' $doc->addScriptDeclaration($js);Ensure you only have 1 jQuery library being imported. If you have more than 1, use the following code to check if there is already a library being imported. If there is, don't include your own, if there isn't, include your own:
<?php // load jQuery, if not loaded before if(!JFactory::getApplication()->get('jquery')){ JFactory::getApplication()->set('jquery',true); $document = JFactory::getDocument(); $document->addScript(JURI::root() . "templates/template_name/js/jquery-1.8.3.js"); } ?>Obviously change the path so whatever suits your needs.
If this doesn't help, ensure you have added you code the view.html.php file correctly like so:
$doc = JFactory::getDocument(); //remove this line if it's already in the file $js = '$.each(respJSON, function(){ $("#poll-questions").append( $('<li>').append( $("<a>").attr("href", prefix + this.id).attr("class", "modal") .attr("rel", "{handler: "iframe", size:{x:600, y:500}}") .append($("<span>").append(this.question) )); });' $doc->addScriptDeclaration($js);更多推荐
发布评论