1
我有预标记之间的一些代码,我要赶人与onclick事件的代码 我的HTML结构如下图所示:如何将一个唯一的ID添加到onclick事件
<div class="Message">
<div class="surroundpre">
<span class="control-copytextarea" onclick="return fieldtoclipboard.copyfield(event, \\\'id1\\\')">[Select and Copy]</span>
<pre class="CodeBlock id="id22015640">
<!-- code goes her -->
</pre>
</div>
</div>
预元素和与类surroundpre的div是由javascript创建的。 用于预的唯一ID:
$('pre').each(function(){
if ($(this).attr('id') == undefined){
$(this).attr('id','id'+Math.floor((Math.random() * 99999999) + 1))
}
});
与surroundpre DIV的是象下面这样创建的:
$('.Message .CodeBlock', this).wrap('<div class=surroundpre></div>');
跨距与PHP变量创建的:
$SelectButton = '<span class="control-copytextarea" onclick="return fieldtoclipboard.copyfield(event, \\\'id1\\\')">[Select and Copy]</span><br />';
结合:
$('.surroundpre').prepend('$SelectButton');
我的问题:在php变量中的id1应替换为与pre标签中相同的唯一标识。 我该如何做到这一点? 或者还有其他方法来实现这一点?
使用'random'生成唯一的ID可能不如简单的计数器更可取。 IE'var nextUniqueIDCounter = 0;'然后'$(this).prop('id','id'+ nextUniqueIDCounter ++);' – teynon
你说'php变量'...是'$ SelectButton =' [Select and Copy]
';'在PHP中生成?你打电话过得怎么样?它是否在页面加载之前被调用? – teynon
是的,所有这些代码都在公共函数中;和javascript是在.livequery(函数()一个PHP变量的大字符串:$结果=“jQuery(文档).ready(函数($){.....等等...... – nuet