2012-07-30 53 views
0

我需要在可编辑的iframe中选择imgs进行一些操作。我可以做到这一点与所有图像(这里是代码,例如) $(iframe).contents().find('img').attr('alt', 'new alt')但我只需要img WITHIN用户选择。请帮帮我。由于jquery如何操作可编辑iframe中的用户选择中的img属性

当用户选择让你的代码中选择图像时使用

//inserted within your selection method 
$(iframe).contents().find('img').removeClass('selectionClass'); 
//this removes all previously selected images 
//unless you have a deselection method in which case you don't need this, 
//you would need to add $(this).removeClass('selectionClass') to the deselection 

$(this).addClass('selectionClass'); 
//adds it to currently selected images 

然后选择类添加到用户的选择图像

回答

1

要编辑的使用:

$(iframe).contents().find('img.selectedClass').attr('alt', 'new alt'); 
+0

嗯,这段代码适用于iframe中的所有图片。例如,用户选择带有文本和图像的可编辑iframe内容的片段 - 并且需要使用alt属性alt ='Hello,Im用户选择中的图像进行所有图像的选择。 - 我认为你的代码不能正常工作 – 2012-07-30 12:29:06

+0

如何选择图像? – CKKiller 2012-07-30 13:16:00

+0

图像可以在选定的文本块内,因此需要在用户选择中找到它并使用属性进行一些操作 – 2012-07-30 13:29:49

0

为什么不只是给你的标签给一个id,然后用jQuery调用它,然后设置alt属性。例如:

<img id="myImageId"/> 

$("#myImageId").attr("alt","This is an image!"); 
+0

用户在可编辑的iframe中选择片段 - 如何知道选择内的img ID?两个,三个或更多imgs? – 2012-07-30 12:15:39