vc中如何动态添加js

yanjing_mail 2010-01-20 03:09:49
我在web控件中打开了一个html页面,里面有下面一句代码:
<input type="hidden" id="open_file" title="openfile"/>
因为想隐藏click事件的内容,所有想在加载后动态添加它的click事件,请问如何实现?

我曾经写成<input type="hidden" id="open_file" onclcik="open()" title="openfile"/>这样,然后用insertAdjacentHTML添加一段open()的js函数,操作之后提示对象不存在,应该是用insertAdjacentHTML加入的js,html没有找到,不知道什么原因。
...全文
238 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
lion_wing 2010-02-24
  • 打赏
  • 举报
回复
请参考:《关于insertAdjacentHTML插入脚本》,一定会对你有帮助。

http://blog.csdn.net/lion_wing/archive/2007/09/29/1806530.aspx
lion_wing 2010-02-24
  • 打赏
  • 举报
回复
请参考:《关于insertAdjacentHTML插入脚本》,一定会对你有帮助。http://blog.csdn.net/lion_wing/archive/2007/09/29/1806530.aspx


norsd 2010-01-24
  • 打赏
  • 举报
回复
一般,如果你添加js文件的引用的话, 我尝试下来是不能执行的,如果可以请告诉我.

我的办法是: (伪代码)

oScript = "<script>"
oScript.defer = true;
oScript.text = js.content;

html.head.appendChild(oScript);

代码可以放在NavigateComplete后执行.(此时body还没有诞生,但是head已经存在)
yanjing_mail 2010-01-21
  • 打赏
  • 举报
回复
自顶一下,求解!
yanjing_mail 2010-01-20
  • 打赏
  • 举报
回复

IHTMLElement* petmp =NULL;
......
petmp->insertAdjacentHTML(L"beforeEnd",L"onclick=\"javascript:alert(\'1234\');\"");


找到<input type="hidden" id="open_file" title="openfile"/>后我用insertAdjacentHTML加入了onclick事件,html页面调用时没有响应时怎么回事?难道还要put_onclick?里面参数该怎么写呢?
sandyandy 2010-01-20
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 tr0j4n 的回复:]
C/C++ codeHRESULT IHTMLElement::put_onclick(VARIANT v);
Sets or retrieves a pointer to the event handler function associated with the onclick event.

IHTMLElement::insertAdjacentHTML

C/C++ codeHRESULT insertAdjacentHTML( BSTRwhere,
BSTR html
);

Inserts the given HTML text into the element at the location.

Get the Interface of IHTMLElement of your Button by IHTMLDocument3::getElementById Method.
Returns a reference to the first object with the specified value of the ID attribute.

C/C++ code
HRESULT getElementById( BSTR v,
IHTMLElement**pel
);
[/Quote]

支持这个
wuhuwy 2010-01-20
  • 打赏
  • 举报
回复
帮顶一下
lllyyy2403 2010-01-20
  • 打赏
  • 举报
回复
web控件是指webBrowser?楼主用的哪个版本的VC?是托管的C++吗?
MoXiaoRab 2010-01-20
  • 打赏
  • 举报
回复
HRESULT IHTMLElement::put_onclick(VARIANT v);

Sets or retrieves a pointer to the event handler function associated with the onclick event.

IHTMLElement::insertAdjacentHTML

HRESULT insertAdjacentHTML(          BSTR where,
BSTR html
);


Inserts the given HTML text into the element at the location.

Get the Interface of IHTMLElement of your Button by IHTMLDocument3::getElementById Method.
Returns a reference to the first object with the specified value of the ID attribute.


HRESULT getElementById( BSTR v,
IHTMLElement **pel
);
marrco2005 2010-01-20
  • 打赏
  • 举报
回复
html代码过滤
http://www.cnblogs.com/jcss2008/archive/2009/05/06/1450713.html
你看一下
  • 打赏
  • 举报
回复
会不会是添加的位置不对。。能否把相关的代码贴出来。。这样更容易发现问题。。
yanjing_mail 2010-01-20
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 marrco2005 的回复:]
我觉得可以先得到 html 的源文件, 在内存里修改源文件, 最后在Navigate 到修改后的源文件
[/Quote]
我现在是这么做的,但是觉得不太好,具体哪不好也说不上来。想知道有别的方法吗
marrco2005 2010-01-20
  • 打赏
  • 举报
回复
我觉得可以先得到 html 的源文件, 在内存里修改源文件, 最后在Navigate 到修改后的源文件

3,056

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC HTML/XML
社区管理员
  • HTML/XML社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧