积极答复者
WebBrowser控制Html代码

问题
-
uint nMsg = NativeMethods.RegisterWindowMessageW("WM_HTML_GETOBJECT");
System.IntPtr lpdwResult = (System.IntPtr)0;
NativeMethods.SendMessageTimeoutW(_internet_Explorer_Server, nMsg, 0, 0, NativeMethods.SMTO_ABORTIFHUNG, 60000, out lpdwResult);现在已经该用IE,这个问题已经不管它了。
我现在的遇到困难是
IHTMLElementCollection elements = HTMLDocument.getElementsByTagName("select");
IHTMLSelectElement y = null;
foreach (IHTMLElement element in elements)
{y = (IHTMLSelectElement)element;
y.selectedIndex = 5;
......
目标列表框的显示已经改变,但界面的其他的数据并没有随之改变。网上说应该触发onchange事件,好像并没有效果!
查看了html
<div><select class="xxxx" id="yyyy">
<option>zzzz</option>
......
</select></div>
谢谢!
- 已拆分 ThankfulHeartModerator 2013年9月27日 11:57 和上一个问题不相干,另开一个帖子
答案
-
$('.order-status-filter-box').change(function(){})
这句代码的意思应该是给class为order-status-filter-box的网页元素注册一个change事件。但是这个代码是包含在orderManageStatusFilter这个方法里面的,这个方法应该在class为order-status-filter-box的网页元素change之前执行,比如在页面加载完成之后执行。这样才能在这个select元素选择项改变的时候触发这个事件。
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.- 已标记为答案 东海马 2013年10月17日 3:26
全部回复
-
查看HTML不会反应脚本的更改。
如果网站的脚本里有onchange事件的代码,那么应该通过IHTMLElement3触发onchange事件。这个不会自动触发以避免在onchange里改选择时死循环。
Visual C++ MVP -
你好:
如果你想直接通过代码来操作已经打开的IE浏览器中的html元素的话,我建议你使用SHDocVw.dll(添加对System32\SHDocVw.dll的引用),用它来获取IE对象。
然后通过mshtml命名空间(添加对Microsoft.mshtml的引用)下面的html元素类来操作它们。
请参考下面的代码来实现你的功能:
SHDocVw.InternetExplorer ie = null;//IE object string url = string.Empty;//Current ie page URL SHDocVw.ShellWindows SWs = new SHDocVw.ShellWindows(); foreach (SHDocVw.InternetExplorer internetExplorer in SWs) { url = internetExplorer.LocationURL; ie = internetExplorer; return; } mshtml.IHTMLDocument2 document = ie.Document as mshtml.IHTMLDocument2; var tag = document as mshtml.HTMLDocument; mshtml.IHTMLElementCollection hTMLElementCollection = tag.getElementsByTagName("input"); foreach (mshtml.HTMLSelectElement el in hTMLElementCollection) { switch (el.id) { case "yyyy": el.selectedIndex = 5; //Do something else here... break; } }
Caillen
<THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
Thanks
MSDN Community Support
Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later. -
谢谢各位,我现在的问题时,我改变了selectIndex的值,页面上列表框的选项是改变了,但由此触发其他的数据改变并没有发生。看了很多文档,说应该触发onchange事件,但我
object refObj = null ;
IHTMLEventObj2 EventObj2 = (IHTMLEventObj2) ((IHTMLDocument4)DOC).CreateEventObject(ref refObj) ;
object eventRef = EventObj2;IHTMLElement3 x2 = (IHTMLElement3)element;
x2.FireEvent("onchange", ref eventRef);没有反应。请各位给个思路吧,谢谢。
-
你确定select的onchange事件的方法没有问题吗?
如果方便的话,你还是把你的项目代码打包一下放到skydrive上,并且把你要操作的网页的网址附上。或者你把完整的代码贴出来供大家参考一下。
谢谢!
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey. -
可以啊,你贴出来吧,我可以新建一个临时网站测试代码。
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey. -
<select class="order-status-filter-box oncloud-inline-block" id="order-status-filter" value="10"><option class="value10" selected="selected" value="10">等待确认[0]</option><option class="value20" value="20">确认可以配送[0]</option><option class="value30" value="30">确认无法配送[1]</option><option class="value40" value="40">配送中[1]</option><option class="value100" value="100">交易成功[569]</option><option class="value200" value="200">交易关闭[71]</option></select>
整个页面代码太多,不给贴,这里是我希望改变的SELECT
-
你好:
你应该没有测试我之前回复的代码吧?
我新建了一个页面,给这个页面添加了一个onchange方法,修改你给的下拉列表的selectedindex之后,调用FireEvent事件,是会触发onchange事件的。
SHDocVw.InternetExplorer ie = null;//IE object string url = string.Empty;//Current ie page URL SHDocVw.ShellWindows SWs = new SHDocVw.ShellWindows(); foreach (SHDocVw.InternetExplorer internetExplorer in SWs) { url = internetExplorer.LocationURL; ie = internetExplorer; } mshtml.IHTMLDocument2 document = ie.Document as mshtml.IHTMLDocument2; var doc = document as mshtml.HTMLDocument; mshtml.IHTMLElementCollection hTMLElementCollection = doc.getElementsByTagName("select"); foreach (mshtml.HTMLSelectElement el in hTMLElementCollection) { switch (el.id) { case "order-status-filter": el.selectedIndex = 5; el.FireEvent("onchange"); break; } }
如果你这边测试还是不行的话,有一种可能性就是IE的版本问题。我用IE10测试没有问题,但是网上好像有人用IE9会出现不触发事件的情况,你可以换一下IE版本看看。We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey. -
假如你要操作的网页中的select元素本身就没有onchange方法的话,那调用FireEvent肯定没有效果。
<select class="order-status-filter-box oncloud-inline-block" id="order-status-filter"
value="10" onchange="alert('SelectedIndex has been changed!');"> ... </select>
网页不是你写的话, 那没有办法。
你是想让别人网页上的下拉列表框选项改变的时候出发一些你自己想要的功能是吧?而这个功能网页本身却没有是吧?我觉得这样的话就无解了。你为什么不联系网站开发人员叫他们添加这个功能?
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.
- 已编辑 CaillenModerator 2013年10月11日 1:30 edit
-
我在网页相关的/seller/assets/js/seller.js
中发现
function orderManageStatusFilter(){$('.order-status-filter-box').change(function(){var status = $('option:selected').val();$('.order-status-filter-box').attr('value',status);createOrderManageList(status);});}
不知道是否调用它,就能让界面更新呢!
用了,execScript,没有反应,谢谢
-
$('.order-status-filter-box').change(function(){})
这句代码的意思应该是给class为order-status-filter-box的网页元素注册一个change事件。但是这个代码是包含在orderManageStatusFilter这个方法里面的,这个方法应该在class为order-status-filter-box的网页元素change之前执行,比如在页面加载完成之后执行。这样才能在这个select元素选择项改变的时候触发这个事件。
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.- 已标记为答案 东海马 2013年10月17日 3:26
-
半夜测试成功了。
使用HTMLWindow2.execScript("createOrderManageList(10)");
这个createOrderManageList来自
function orderManageStatusFilter(){$('.order-status-filter-box').change(function(){var status = $('option:selected').val();$('.order-status-filter-box').attr('value',status);createOrderManageList(status);});}
10是option的VAL
谢谢你!